购物车系统
超市买水果功能分析
(1)输入自己所有的钱。
(2)展示商品的序号,名称及其价格。
(3)输入要买商品的序号。
(4)输入要买商品的数量。
(5)购物车中显示购买的水果名称及其对应的数量和剩余钱。
(6)如果序号输入有误就提示用户重新输入。
(7)如果钱不够了提示用户钱不够,并且退出程序。
product_list = [{'name': '苹果', 'price': 10},
{'name': '榴莲', 'price': 30},
{'name': '草莓', 'price': 20},
{'name': '菠萝', 'price': 15},
]
my_money = float(input("我带的钱:"))
for index, num in enumerate(product_list, start=1):
print('水果的序号:{} 水果:{},价格:{}'.format(index, num['name'], num['price']))
while True:
xh=int(input("请输入需要购买水果的序号:"))
if 0<xh<=4:
sl=int(input("请输入购买数量:"))
je = product_list[xh - 1]['price']
zj=sl*je
print("总价格为:",zj)
if zj<=my_money:
ye=my_money-zj
print("您还剩余%d元"%ye)
my_money-=zj
else:
print("余额不足!")
break
else:
print("输入有误,重新输入")