2014年5月24日土曜日

開発環境

Head First Programming A learner's guide to programming using the Python language (David Griffiths(著)、Paul Barry(著)、 O'Reilly Media; )のChapter 6(Modular Programming: Keeping things straight)、Sharpen your pencil(p.201)を解いてみる。

Sharpen your pencil(p.201)

コード(BBEdit)

promotion.py

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

def discount(price):
    return price * 9 / 10

coffee_pos.py

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

import transactions
import promotion

items = ['WORKOUT', 'WEIGHTS', 'BIKES']
price = [1.50, 2.20, 1.80, 1.20]
running = True

while running:
    option = 1
    for choice in items:
        print('{0}. {1}'.format(str(option), choice))
        option = option + 1
    print('{0}. Quit'.format(str(option)))
    choice = int(input('Choose an option: '))
    if choice == option:
        running = False
    else:
        credit_card = input('Credit card number: ')
        transactions.saveTransaction(promotion.discount(price[choice - 1]),
                                     credit_card, items[choice - 1])

入出力結果(Terminal)

$ ./coffee_pos.py
1. WORKOUT
2. WEIGHTS
3. BIKES
4. Quit
Choose an option: 2
Credit card number: 1234567890123456
1. WORKOUT
2. WEIGHTS
3. BIKES
4. Quit
Choose an option: 4
$ cat transaction.txt 
12345678901234560000150           DONUT
65432109876543210001000         WEIGHTS
12345678901234560000198         WEIGHTS
$ 

0 コメント:

コメントを投稿