1 # -*- coding:utf-8 -*- 2 shp = [ 3 ['iphone',5000], 4 ['offee',35], 5 ['shoes',800] 6 ] 7 pric_list = [] 8 e = int(raw_input("请输入账户金额:")) 9 while True: 10 for i,p in enumerate(shp): 11 print i,p[0],p[1] 12 enther = raw_input("请输入要购买的商品序号:").strip() 13 if enther.isdigit(): 14 enther = int(enther) 15 #p_price = products[choice][1] 16 pric = shp[enther][1] 17 if pric <= e: 18 pric_list.append(shp[enther]) 19 e -= pric 20 print "您购买的%s已添加到购物车,剩余金额%s" %(shp[enther][0],e) 21 else: 22 print "金额不足,请重新选择,剩余金额%s" % e 23 elif enther == 'quit': 24 print "购买的商品列表" 25 for k,v in enumerate(pric_list): 26 print k,v 27 print "剩余金额为%s" % e 28 print "-----欢迎再次光临------" 29 break
实现了输入金额后购买商品直到金额不足