day06_04 购物车讲解02

1.0 补充知识

a,b = [2,3]
print(a)
print(b)
#>>>2
#>>>3

  

a,b = (2,3)
print(a)
print(b)
#>>>2
#>>>3

  

2.0 购物车作业:

product_list = [
    ('mac',9000),
    ('kindle',800),
    ('tesla',900000),
    ('python book',105),
    ('bike',2000)
]

saving = input("please input your saving:") #请输入你储蓄卡里有多少钱?
shopping_car = []
if saving.isdigit(): #验证输入的是否是数字
    saving = int(saving)

    # for i in product_list:
    #     print(product_list.index(i),i)

    while True:
        #打印商品内容
        for i,v in enumerate(product_list,1): #请参考1.0补充知识
            print(i,'>>>',v)
        #引导用户选择商品
        choice = input('选择购买商品的编号[退出:q]:')

        #验证输入是否合法
        if choice.isdigit():
            choice = int(choice)
            if 0 < choice <= len(product_list) :
                #将用户选择的商品通过choice取出来
                p_item = product_list[choice-1]
                #如果钱够,用本金saving减去该商品价格,并将该商品加入购物车
                if p_item[1]<saving:
                    saving -= p_item[1]
                    shopping_car.append(p_item)
                else:
                    print('余额不足,还剩%s'%saving)
                print(p_item)
            else:
                print("编码不存在")
        elif choice == 'q':
            print('----您已经购买如下商品----')
            #循环遍历购物车里的商品,购物车存放的是已买的商品
            for i in shopping_car:
                print(i)
            print("您还剩%s元钱"%saving)
            break
        else:
            print("invalid input")

  

posted on 2017-09-09 10:55  darkalex001  阅读(132)  评论(0编辑  收藏  举报

导航