大鹏

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
product_list=[
    ('book',100),
    ('Mac Pro',9000),
    ('watch',500),
    ('coffee',30),
    ('Python',106),]

saving=input('input your saving:')
shopping_car=[]

if saving.isdigit():
    saving=int(saving)
    while True:
        for i,v in enumerate(product_list):
            print(i,v)
        user_choice=input('选择购买商品编号[退出:q]:')

        if user_choice.isdigit():
            user_choice=int(user_choice)
            if user_choice<len(product_list) and user_choice>=0:
                product_item=product_list[user_choice]
                if product_item[1]<saving:
                    saving-=product_item[1]
                    shopping_car.append(product_item)
                    print('您当前的余额为%s'%saving)
            else:
                print('编号错误')
        elif user_choice=='q':
            print('---------您已经购买如下商品-----------')
            for i in shopping_car:
                print(i)
            print('您的余额为%s'%saving)
            break

        else:
            print('invalid choice')

 

posted on 2018-12-16 19:11  pf42280  阅读(93)  评论(0编辑  收藏  举报