2014年5月25日日曜日

開発環境

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)、LONG EXERCISE(p.208)を解いてみる。

LONG EXERCISE(p.208)

コード(BBEdit)

coffee_pos.py

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

import transactions
import promotion
import starbuzz

items = ['DONUT', 'LATTE', 'FILTER', 'MUFFIN']
prices = [1.50, 2.0, 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: ')
        card = input('Do you have a Starbuzz discount Card?(Y/N): ')
        price = promotion.discount(prices[choice - 1])
        if card == 'Y':
            price = starbuzz.discount(price)
        transactions.saveTransaction(price, credit_card, items[choice - 1])

入出力結果(Terminal)

$ ./coffee_pos.py
1. DONUT
2. LATTE
3. FILTER
4. MUFFIN
5. Quit
Choose an option: 2
Credit card number: 1234567890123456
Do you have a Starbuzz discount Card?(Y/N): Y
1. DONUT
2. LATTE
3. FILTER
4. MUFFIN
5. Quit
Choose an option: 2
Credit card number: 6543210987654321
Do you have a Starbuzz discount Card?(Y/N): N
1. DONUT
2. LATTE
3. FILTER
4. MUFFIN
5. Quit
Choose an option: 5
$ cat transaction.txt 
12345678901234560000171           LATTE
65432109876543210000180           LATTE
$ 

0 コメント:

コメントを投稿