购物车[Python基础]
需求:
- 启动程序后,让用户输入工资,然后打印商品列表
- 允许用户根据商品编号购买商品
- 用户选择商品后,检测余额是否够,不够充值,够就直接扣款
- 账号余额刚好是0时,直接退出
- 可随时退出,退出时,打印已购买商品和余额
1# Author:Zhao2 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
7while count < 1:
8for i in commodity:
9print(i)
10 num = int(input("请输入你要购买的商品编号: "))
11#购买商品的金额
12 amount = commodity[num - 1][2]
13if salary >= amount:
14 salary = salary - amount
15 purchased.append(commodity[num - 1][1])
16print("购物车已加入:", purchased, "余额还剩:", salary)
17else:
18 salary2 = amount - salary
19print("购买",commodity[num -1][1],"商品","还差",salary2,"元,","请充值......")
20 salary1 = int(input("请输入充值金额: "))
21 salary = salary+salary1
22if salary >= amount :
23 salary = salary -amount
24 purchased.append(commodity[num - 1][1])
25print("购物车已加入:", purchased, "余额还剩:", salary)
26else:
27print("余额不足,选购其他商品吧")
28 count += 1
29if count == 1:
30if salary == 0:
31print("你的钱包已经被掏空")
32break
33 countine_confirm = input("是否继续购物:")
34if countine_confirm !="q" :
35 count =0
36print("已选购了:",purchased,"余额还剩:",salary)
以上是 购物车[Python基础] 的全部内容, 来源链接: utcz.com/z/530067.html