要求
代码:
b = 1
my_money = int(input('请输入您的总资产:'))
cars = []
goods = [
{'name':'电脑','price':1999},
{'name':'鼠标','price':10},
{'name':'游艇','price':20},
{'name':'美女','price':998}
]
print('商品列表:')
for good in goods:
print('{}:{}元'.format(good['name'],good['price']))
while True:
my_good = input('请输入需要加入购物车的商品(按"Q"退出):')
if my_good.upper() == 'Q':
break
lenth = len(goods) # lenth = 4
for i in range(lenth):
if my_good == goods[i]['name']: #如果 输入的商品 等于 goods列表中的一个商品
good_money = goods[i]['price']
if good_money > my_money:
print('您的余额不够了!')
break
else:
cars.append(goods[i])
my_money = my_money - good_money
print('加入成功!')
print(cars)
break
else:
print('您输入的商品不存在!')
# if b == 0:
# print('您输入的商品不存在!!')
print('您已退出')