写一个循环,不断的问客户想买什么 ,当用户选择一个商品编号,就把对应的商品加入购物车 ,最终用户输入q退出时,答应购物车的商品

写一个循环,不断的问客户想买什么 ,当用户选择一个商品编号,就把对应的商品加入购物车 ,最终用户输入q退出时,答应购物车的商品
products=[["Iphone8",68888],["MacpPro",14800],["Coffee",31],["小米",2499],["Book",80],["Nlke",799]]
shopping_cart=[]
print(".....商品信息......")
#while True:
while True: #条件一直是真
for index,i in enumerate(products): #enumerate 是枚举
print("%s. %s %s"%(index,i[0],i[1])) #占位符格式化输出
choices=input("请输入你想购买的商品编号:") # 输入的值赋值给一个变量
if choices.isdigit(): #isdigit是判断是数字 如果输入的数字 则choices强制转换为int
# print(len(products)) #len() 取列表的长度
choices=int(choices)
if choices>0 and choices<6: #choices的长度 要小于长度
shopping_cart.append(products[choices]) #把输入的商品编号对应商品存到一个列表
print("商品%s已加入购物车"%(products[choices])) #打印商品加入购入车
else:
print("输入的商品存在 ")
elif choices=='q':
if len(shopping_cart)>0:
print("......购物车的商品........")
for index,i in enumerate(shopping_cart):
print("%s. %s %s "%(index,i[0],i[1]))
else:
print("还没有购买商品")
break
else:
print("输入商品编号或者退出")

猜你喜欢

转载自www.cnblogs.com/chenjiao0904/p/9689693.html
今日推荐