products = [["iphone",6888],["MacPro",14800],["小米6",2499],["Coffee",31],["Book",60],["Nike",699]]
print("----- 商品列表 -----")
i = 0
for product in products:
# print(i,"%s"%product[0],"%d"%product[1])
print("%d\t%s\t%d"%(i,product[0],product[1]))
i += 1
shoppingTrolley = []
choice = input("请选择需要购买的商品,商品编号:0-5,按q退出:")
while choice != "q":
choiceNumber = int(choice)
shoppingTrolley.append(products[choiceNumber])
print(shoppingTrolley)
choice = input("请选择需要购买的商品,商品编号:0-5,按q退出:")
else:
if shoppingTrolley == []:
print("购物车为空,请选择商品!")
else:
print(shoppingTrolley)
----- 商品列表 -----
0 iphone 6888
1 MacPro 14800
2 小米6 2499
3 Coffee 31
4 Book 60
5 Nike 699
请选择需要购买的商品,商品编号:0-5,按q退出:q
购物车为空,请选择商品!
请选择需要购买的商品,商品编号:0-5,按q退出:1
[['MacPro', 14800]]
请选择需要购买的商品,商品编号:0-5,按q退出:3
[['MacPro', 14800], ['Coffee', 31]]
请选择需要购买的商品,商品编号:0-5,按q退出:2
[['MacPro', 14800], ['Coffee', 31], ['小米6', 2499]]
请选择需要购买的商品,商品编号:0-5,按q退出:q
[['MacPro', 14800], ['Coffee', 31], ['小米6', 2499]]