python小程序之购物系统
1 # -*- coding: utf-8 -*- 2 import time 3 goods = [ 4 {"name":"computer","price":1999}, 5 {"name":"mouse","price":19}, 6 {"name":"Yacht","price":199}, 7 {"name":"human","price":999}, 8 ] 9 shop = [] 10 money = 0 11 while True: 12 print "1充值,2添加商品,3移除商品,4查看购物车,5查看余额,6结账 " 13 inp = input('请选择您需要的操作:') 14 num = int(inp) 15 if num == 1: 16 inp = input("请输入充值钱数:") 17 money = money + int(inp) 18 print "当前余额:",money 19 time.sleep(1) 20 elif num == 2: 21 for key, item in enumerate(goods,1): 22 print key,item 23 inp = input("请输入购买商品序号:") 24 num = int(inp) 25 shop.append(goods[num - 1]) 26 print "您添加的商品为:",goods[num - 1] 27 time.sleep(1) 28 elif num == 3: 29 for key, item in enumerate(shop,1): 30 print key,item 31 inp = input("请输入你要移除的商品序号:") 32 num = int(inp) 33 shop.remove(shop[num -1]) 34 elif num == 4: 35 print shop 36 elif num == 5: 37 print money 38 else: 39 inp = 0 40 sum = 0 41 while inp < len(shop): 42 sum = sum + shop[inp]['price'] 43 inp += 1 44 if money < sum: 45 print "余额不足请充值!" 46 else: 47 print"结账成功!" 48 break