购物车 python作业
功能要求:
要求用户输入总资产,例如:2000
显示商品列表,让用户根据序号选择商品,加入购物车
购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。
附加:可充值、某商品移除购物车
goods = [
{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998},
]
goods = [
{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998},
]
zzc = int(input('请输入总资产:'))
car = []
while True:
for i1 in enumerate(goods):
print(i1[0],i1[1].get('name'),i1[1].get('price'))
ret = int(input('请根据序号选择商品,退出输入88:'))
for i2 in enumerate(goods):
if i2[0] == ret:
car.append(goods[ret])
print(car)
if ret == 88:
for i3 in car:
zj = 0
zj = i3['price'] + zj
if zj > zzc:
print('余额不足,请充值')
# xz = input('余额不足,选择充值输入y,退出购买输入n:')
# if xz == 'y':
# jj = int(input('请输入充值金额:'))
# zzc = jj + zzc
# else:
# print('欢迎下次光临')
else:
print('购买成功')
break