python学习-购物车

hcwei 2020年06月10日 70次浏览
#hcwei's first python
#data:2018-11-23 17:40
pro_list=[('iphone',5000),('macbook',9000),('ipad',2000),('bike',800),('mouse',50)]
buy_list=[]
#for item in pro_list:
#    print(pro_list.index(item),item)
money=int(input("请输入您的工资:"))
while True:
  for index, item in enumerate(pro_list):
     print(index, item)
  choice=input("选择要购买的商品:")
  if choice.isdigit():
     choice=int(choice)
     if choice > len(pro_list) or choice < 0: print("商品不存在") else: if money >= pro_list[choice][1]:
           money-=pro_list[choice][1]
           buy_list.append(pro_list[choice])
           print('已选购列表:',buy_list,'剩余金额:',money)
        else:
           print("没钱了还买个锤子")
           print('---------------buy list---------------')
           print(buy_list)
           print('余额为:',money)
  elif choice=='q':
      print('---------------buy list---------------')
      print(buy_list)
      print('余额为:', money)
      exit()
  else:
    print("请输入商品编号")