功能要求:
1.要求用户输入自己拥有的总资产,例如:30000
2.显示商品列表的序号,商品名称,商品价格,让用户根据序号选择商品,然后加入购物车
例如:
1 Macbook 12000
2 Logines 8730
3 Villa 650000
3.用户可以多次购买商品
4.用户输入q退出|输入n结算
5.结算的时候如果商品总额大于总资产,提示账户余额不足,否则,购买成功。
trades = [
{"trade_name": "Macbook", "value": 12000},
{"trade_name": "Gucci-glasses", "value": 6388},
{"trade_name": "Logines", "value": 8730},
{"trade_name": "Girlfriend", "value":23000},
{"trade_name": "Tesla", "value": 476000},
{"trade_name": "Diamond-ring", "value": 18800},
{"trade_name": "Villa", "value": 6500000},
]
trades = [
{"trade_name": "Macbook", "value": 12000},
{"trade_name": "Gucci-glasses", "value": 6388},
{"trade_name": "Logines", "value": 8730},
{"trade_name": "Girlfriend", "value":23000},
{"trade_name": "Tesla", "value": 476000},
{"trade_name": "Diamond-ring", "value": 18800},
{"trade_name": "Villa", "value": 6500000},
]
flag = 1
shopping_cart = {}
usr_fund = 0
while flag:
fund = input('尊敬的客户,请输出您的资产: ').strip()
if fund.isdigit():
usr_fund += int(fund) # 得到用户的总资产
break
else:
print('您输入的金额有误,请重新输入')
while flag:
for i in trades:
print(range(len(trades)),i['trade_name'],i['value'])
# 取得商品序号,商品和价钱
res = input('请您输入商品序号或者N/Q结算或者退出: ').strip()
if res.upper() =='Q': # 执行退出操作
exit('有需要再来光顾哦!')
elif res.upper() =='N': #执行结算操作
while flag:
if usr_fund < sum(shopping_cart.values()):
# 如果用户金额小于结算金额
cmd = input(f"您还差{(sum(shopping_cart.values()) - usr_fund)}元才能付款,输入1/0可以选择充值或取消购买").strip()
if cmd == '':
while 1:
cmd_1 = input('充值金额: ').strip()
if cmd_1.isdigit():
usr_fund += int(cmd_1)
break
else:
print('您输入的有误,请重新输入')
elif cmd == '':
exit('有需要再来光顾哦!')
else:
print('您输入的有误,请重新输入')
else:
print('您的购物车现在有: ')
for i in shopping_cart:
print(i,shopping_cart[i])
print(f"您的余额还有{usr_fund - sum(shopping_cart.values())}元")
break
break
elif res.isdigit(): #如果用户输入的是序号,那就加入购物车
res_1 = int(res)
if res_1 - 1 in [range(len(trades)) for i in trades]: #如果用户输入的序号在商品列表中
if trades[res_1]['trade_name'] not in shopping_cart:
shopping_cart.setdefault(trades[res_1 -1]['trade_name'],trades[res_1 -1]['value'])
else:
shopping_cart[trades[res_1 -1]['trade_name']] += trades[res_1 -1]['value']
print('\n'f"{trades[res_1 -1]['trade_name']}已添加到购物车,价值{trades[res_1 -1]['value']}元"'\n')
else:
print('您输入的有误,请重新输入')
else:
print('您输入的有误,请重新输入')