嵌套列表实现购物车项目

嵌套列表实现购物车项目

#_author:Administrator
#date:2019/10/25
#购物车程序
salary=int(input("please input your salary:"))
print("购物清单如下")
msg='''
---------购物清单---------
1.iphone11 5800
2.mac book 9000
3.coffee 38
4.python book 80
5,bicycle 1200
--------------------------
'''
print(msg)
balance=0
shopping_car=[]

while True:
i = int(input("请输入你要购买的商品编号:"))

super_shopping=[[],['iphone11',5800],['mac book ',9000],['coffee ',38],['python book',80],['bicycle',1200]]
balance = salary - super_shopping[i][1] #list[i]
if balance<0:
print("你的余额不足为,%d,请重新选择:你还需要购买东西吗?[yes/no]"%balance)

say=input("请输入:")
if say=="yes":

continue
else:
print("欢迎再次光临")

print("你的购物车有如下商品:")
print(shopping_car)
break



else:
shopping_car.append(super_shopping[i][0])#将该商品加入域购物车

print("该商品已加入购物车,当前余额为%d"%balance)
print("您是否要去付款呢?[yes/no]")
to_pay=input("请输入:")
if to_pay=='yes':
salary=balance
continue
else:
#此处可以一一打印购买的商品
print("你还需要购买其他商品吗?[yes/no]")
answer=input("your answer:")
if answer=="yes":
salary=balance
if salary<0:
break

else:
continue
else:
print("欢迎您的光临")
print("你的购物车有如下商品:")
print(shopping_car)
break
Output:

please input your salary:5880
购物清单如下

---------购物清单---------
1.iphone11 5800
2.mac book 9000
3.coffee 38
4.python book 80
5,bicycle 1200
--------------------------

请输入你要购买的商品编号:1
该商品已加入购物车,当前余额为80
您是否要去付款呢?[yes/no]
请输入:yes
请输入你要购买的商品编号:4
该商品已加入购物车,当前余额为0
您是否要去付款呢?[yes/no]
请输入:yes
请输入你要购买的商品编号:1
你的余额不足为,-5800,请重新选择:你还需要购买东西吗?[yes/no]
请输入:no
欢迎再次光临
你的购物车有如下商品:
['iphone11', 'python book']

原文地址:https://www.cnblogs.com/startl/p/11739156.html