Python专辑

1.第一个购物车小程序

2016-11-13  本文已影响204人  蜗牛仔
product_list = [
    ('iphone',5800),
    ('mac book',12000),
    ('Coffee',33),
    ('Jay python',130),
    ('bike',800),
    ('watch',1200),
]
shopping_list=[]
salary=input('请输入你的工资:')
if salary.isdigit():
    while True:
        salary = int(salary)
        for index,item in enumerate(product_list):
            print(index,item)
        Use_choice= input("请输入你要购买的商品序号:")
        if Use_choice.isdigit():
            Use_choice= int(Use_choice)
            if Use_choice <len(product_list) and Use_choice>=0:
                p_item = product_list[Use_choice]
                if salary>p_item[1]:
                    salary-=p_item[1]
                    shopping_list.append(p_item)
                    print('Add %s into shopping list,and your current balance is %s'%(p_item,salary))
                else:
                    print('你就这点剩%s这点钱了,还买个毛线啊'%(salary))
            else:
                print('你要购买的商品不存在')
        elif Use_choice =='q':
            print('---------shopping list-------')
            for item in shopping_list:
                print(item)
            print('你现在的余额是{}'.format(salary))
            break
        else:
            print('无效输入,请重新选择商品序号或者按q退出')```
上一篇下一篇

猜你喜欢

热点阅读