以前的小练习

#定义一个商品列表
#列表嵌套
product_list = [["pybooks",80],["bicycle",1500],["Iphone",5000],["watches",1000],["necklace",2000]]
shoppingcarts = []
#输入存储金额
saving = input("请输入存储金额:")
if saving.isdigit():
    saving = int(saving)
    for i,v in enumerate(product_list,1):              #添加序号
      print(i,v)
    while True:
        choice = input("请选择序号:")
        if choice.isdigit():
            choice = int(choice)
            if choice >0 and choice <= len(product_list):
                #对比金额是否足够
                balance = product_list[choice-1]  #取商品
                if balance[1] < saving:
                    saving -= balance[1]
                    shoppingcarts.extend(balance)  # 把序号对应的商品添加到购物车中
                else:
                    print("您的余额不足,还剩余 %s" %saving)
                print(balance)    #打印商品
            else:
                print("您输入的编码没有!")
        elif choice=='q':
            for i in shoppingcarts:  #打印全部购买的商品
                print(i)
            print("您还剩余 %s" %saving)
            break
        else:
            print("非法输入")
else:
    print("请输入金额数字")

 

你可能感兴趣的:(以前的小练习)