【今日代码段】--购物车

【购物车】

  

# __author:"Will Z"
# date:2016/8/22
product_list = [('mac     ',9000),('kindle  ',800),('computer',200)]
your_money = input("请输入您的工资: ")
shopping_car = []

if your_money.isdigit():    #判断是否是数字
    your_money = int(your_money)
    while True:
    #for i in product_list:
        #print(product_list.index(i+1),i)
    #for i in enumerate(product_list,1):
        #打印商品内容
        print("-----欢迎光临,本店商品如下-----")
        for i,(v,w) in enumerate(product_list,1):   #enumerate是列举的意思,在列表前打印序号,1代表从1开始打印,默认是0,(v,m)代表拆分元组分别打印元组里的内容

            print(i, v, '			', w)
        #引导用户选择商品
        choose = input('选择购买商品的编号[退出请输入:q]:')
        #验证输入是否合法
        if choose.isdigit():
            choose = int(choose)
            if choose > 0 and choose <= len(product_list):
                #将用户选择商品通过choose取出来
                p_item = product_list[choose-1]
                #如果余额充足,用余额减去商品价格,并将商品加入购物车
                if p_item[1] < your_money: #判断余额是否足够购买商品
                    your_money -= p_item[1]
                    shopping_car.append(p_item) #将购买的商品追加到已购买的商品里面
                else:
                    print("余额不足,当前余额为:%s"%your_money)
            else:
                print('编码不存在')

        elif choose == "q":
            print('----您已经购买如下商品------')
            print('商品		 价格')
            #循环遍历购物车里面的商品,购物车存放的是已购买的商品
            for i,v in shopping_car:
                print(i,'	',v)
            print("您的当前余额为:%s"%your_money)
            break
        else:
            print('非法字符')
人生短短数十载,经不起几次重头再来
原文地址:https://www.cnblogs.com/bk770466199/p/5799118.html