python元组与购物车程序

#Author:zww
'''
程序:购物车程序
需求:
     1.启动程序后,让用户输入工资,然后打印呢商品列表
     2.允许用户根据商品编号购买商品
     3.用户选择商品后,检测余额是否足够,够就直接扣款,不够就提醒
     4.可随时退出,退出时,打印购买的商品和余额
'''
product_list = [
    ('iphone7',6000),
    ('book',150),
    ('bike', 1000),
    ('bag', 300),
]
shopping_list = []
salary = input("Input you salary please:")
if salary.isdigit():
    salary = int(salary)
    while True:
        for index,item in enumerate(product_list):
            print(index,item)
        user_choise = input("你要买什么?>>>:")
        if user_choise.isdigit():
            user_choise = int(user_choise)
            if user_choise < len(product_list) and user_choise >=0:
                p_item = product_list[user_choise]
                if p_item[1] <= salary:
                    shopping_list.append(p_item)
                    salary -= p_item[1]
                    print("买了 %s ,还剩下 %s元" % (p_item,salary))
                else:
                    print("你的余额剩下 %s 元,买个毛线" %salary )
            else:
                print("你选择的编号 %s 无效" %user_choise)
        elif user_choise == 'q':
            print("-------shopping list-------")
            for p in shopping_list:
                print(p)
            exit()
        else:
            print("invaliad option")

 

posted @ 2017-04-25 16:01  z寒江雪  阅读(457)  评论(0编辑  收藏  举报