2015年10月15日木曜日

開発環境

  • OS X El Capitan - Apple (OS)
  • Emacs (Text Editor)
  • Python 3.5 (プログラミング言語)

Automate the Boring Stuff with Python: Practical Programming for Total Beginners (Al Sweigart (著)、No Starch Press)のPart Ⅰ.(Python Programming Basics)、Chapter 5.(Dictionaries and Structuring Data)、Practice Projects: Practice Projects(Fantasy Game Inventory)を解いてみる。

Practice Projects: Practice Projects(Fantasy Game Inventory)

コード(Emacs)

#!/usr/bin/env python3
#-*- coding: utf-8 -*-

def display_inventory(inventory):
    print("Inventory:")
    total = 0
    for k, v in inventory.items():
        print('{0} {1}'.format(v, k))
        total += v
    print('\nTotal number of items: {0}'.format(total))
    
inventory = dict(rope=1, torch=6, dagger=1, arrow=12)
inventory.update({'gold coin': 42})

display_inventory(inventory)

入出力結果(Terminal, IPython)

$ ./sample.py
Inventory:
6 torch
12 arrow
1 dagger
42 gold coin
1 rope

Total number of items: 62
$

0 コメント:

コメントを投稿