购物车作业
li=[{'name':'苹果','price':10,},
{'name':'香蕉','price':20,},
{'name':'西瓜','price':30,}]
print('欢迎进入蓝色水果店')
money=input('请输入你所拥有的钱:')
shopping_car= {}
if money.isdigit() and int( money ) > 0:
while 1:
for i, k in enumerate(li):
print('序号{},商品名称{}.商品价格{}'.format(i, k['name'], k['price']))
choose = input('请输入你想要购买的商品序号')
if choose.isdigit() and int(choose) < len(li):
num = input('请输入你需要购买的商品数量')
if num.isdigit():
if li[int(choose)]['price'] * int(num) <=int(money):
money = int(money) - li[int(choose)]['price'] * int(num)
if li[int(choose)]['name'] in shopping_car:
shopping_car[li[int(choose)]['name']]=shopping_car[li[int(choose)]['name']]+int( num )
else:
shopping_car[li[int(choose)]['name']]=int(num)
print('你的购物车中商品有{},剩余余额为{}'.format(shopping_car,money))
else:
print('对不起,你带的钱不够,滚好吗')
break
else:
print('让你输入商品数量,还要我安排尼玛?')
else:
print('请你输入正确的序号')
PS:一、li=[{},{}],列表里面包含字典
二、isdigit()函数判断数字类型
三、li[int(choose)]['price']
四、for i, k in enumerate(li):
print('序号{},商品名称{}.商品价格{}'.format(i, k['name'], k['price']))

浙公网安备 33010602011771号