天天看點

python 購物

# -*- coding: utf-8 -*-

import sys

salary = int(raw_input('Please input your salary:'))

products = [

    ['Iphone', 5800],

    ['MacPro', 12000],

    ['NB Shoes', 680],

    ['Cigarate', 48],

    ['MX4', 2500],

]

list = []

for i in products:

    list.append(i[1])

if salary < min(list):

    print "工資太低,買不起任何東西"

    sys.exit()

low = min(list)

print low

shoplist = []

sum = 0

while True:

    for p in products:

        print products.index(p) ,p[0], p[1]

    choice = int(raw_input("Please choice sth to buy:"))

    if salary > products[choice][1]:

        balance = salary - products[choice][1]

        salary = balance

        print '你選擇 %s 價格是 %s 您的餘額是 %s '% (products[choice][0] ,products[choice][1] ,balance)

        shoplist.append(products[choice][0])

        sum = sum + products[choice][1]

    else:

        if salary > low:

            print "請選擇有能力購買的商品。"

            continue

        print "金額不足,買不到東西了!\n您現在的購物車 %s 總額 %s" % (shoplist ,sum)

        sys.exit()

本文轉自 freeterman 51CTO部落格,原文連結:http://blog.51cto.com/myunix/1907707,如需轉載請自行聯系原作者