功能要求:
v = 2000
要求用户输入总资产,例如:2000
显示商品列表,让用户根据序号选择商品,加入购物车
购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。
goods_i = [
['电脑', 6500],
['鼠标', 1200],
['游艇', 99],
['美女', 998],
['Bike', 999],
]
1 #/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 # Author:ylw 4 5 goods_i = [ 6 ['电脑', 6500], 7 ['鼠标', 1200], 8 ['游艇', 99], 9 ['美女', 998], 10 ['Bike', 999], 11 ] 12 shopping_cart = [] 13 14 salary = int (input("输入你的工资:")) 15 16 while True: 17 for index, i in enumerate(goods_i): 18 print("%s. %s %s" % (index, i[0], i[1])) 19 20 choice = input(">>>:").strip() 21 if len(choice) == 0: 22 print('输入有误,请重新输入!') 23 continue 24 if choice.isdigit(): 25 choice = int(choice) 26 if choice < len(goods_i) and choice >=0: 27 goods_item = goods_i[choice] #获取商品 28 if salary >= goods_item[1]: #买的起 29 salary -= goods_item[1] #扣钱 30 shopping_cart.append(goods_item) 31 print("以购买商品%s,您的余额还剩:%s" %(goods_item[0], salary)) 32 print("商品购买完成输入exit退出") 33 34 else: 35 print("对不起您的余额不足,还需", goods_item[1] - salary) 36 else: 37 print("没有此商品,请重新输入!") 38 elif choice == "exit": 39 total_cost = 0 40 print("您购买了以下产品:") 41 for i in shopping_cart: 42 print(i) 43 total_cost += i[1] 44 45 print("商品总价格:",total_cost) 46 print("您当前的余额为:",salary) 47 print("欢迎下次光临!拜拜再见,古德拜!") 48 break