需求:
- 启动程序后,让用户输入工资,然后打印商品列表
- 允许用户根据商品编号购买商品
- 用户选择商品后,检测余额是否够,不够充值,够就直接扣款
- 账号余额刚好是0时,直接退出
- 可随时退出,退出时,打印已购买商品和余额
1 # Author:Zhao
2 purchased = []
3 salary = int(input("请输入你的工资: "))
4 commodity = [[1,"IPhone",5800],[2,"Mac Pro",12000],[3,"Stacbuck Latte",31],[4,"Alex Python",81],[5,"Bike",800]]
5 count = 0
6 Recharge = 0
7 while count < 1:
8 for i in commodity:
9 print(i)
10 num = int(input("请输入你要购买的商品编号: "))
11 #购买商品的金额
12 amount = commodity[num - 1][2]
13 if salary >= amount:
14 salary = salary - amount
15 purchased.append(commodity[num - 1][1])
16 print("购物车已加入:", purchased, "余额还剩:", salary)
17 else:
18 salary2 = amount - salary
19 print("购买",commodity[num -1][1],"商品","还差",salary2,"元,","请充值......")
20 salary1 = int(input("请输入充值金额: "))
21 salary = salary+salary1
22 if salary >= amount :
23 salary = salary -amount
24 purchased.append(commodity[num - 1][1])
25 print("购物车已加入:", purchased, "余额还剩:", salary)
26 else:
27 print("余额不足,选购其他商品吧")
28 count += 1
29 if count == 1:
30 if salary == 0:
31 print("你的钱包已经被掏空")
32 break
33 countine_confirm = input("是否继续购物:")
34 if countine_confirm !="q" :
35 count =0
36 print("已选购了:",purchased,"余额还剩:",salary)
hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 »
购物车