Python购物车程序
1.要求用户输入工资,然后打印购物菜单
2.用户可以不断的购买商品,直到钱不够为止
3.退出时格式化打印用户已购买的商品和剩余金额
salary = int(input("请输入你的工资:"))
shop_price = 0
shop_list =[["a",100],["b",50],["c",76],["d",229],["e",21]]
shouhuodan = []
for i in range(5):
print("代码",i,"商品名称:",shop_list[i][0],"商品价格",shop_list[i][1])
a = input("是否开始选择:")
while (a!="no"):
shop_trade = int(input("请选择购买的商品代码:"))
shouhuodan.extend(shop_list[shop_trade])
shop_price = shop_price + shop_list[shop_trade][1]
if shop_price < salary:
print("余额;",salary - shop_price)
else:
if len(shouhuodan)==2:
shouhuodan = []
else:
shop_price = shop_price - shouhuodan[-1]
shouhuodan.pop()
shouhuodan.pop()
print("你选择的商品已超出余额限制!")
a = input("是否继续购买:")
if (len(shouhuodan)==0):
print("商品名称:",0, "商品价格", 0)
else:
print("商品",shouhuodan[::2],"价格",shouhuodan[1::2])