商品购买
大概:选择一个商品的编码,对应添加的购物车,用户q打印输出买了什么多少钱退出
- 先输出商品列表
print('--------商品列表------------')
products = [['iphone', 6888], ['MacPro', 14800], ['小米6', 2499], ['Coffee', 31], ['Book', 60], ['Nike', 199]]
i = 0
for lst in products:
print("{0}\t{1}\t{2}".format(i, lst[0], lst[1]))
i += 1
总结构:
goods = []
while True:
try:
choice = (input("请输入你想买的商品:")) # 用户选择
if choice == "q":
answer = input('你确定不购买吗?y/n') # 判断为0
if answer == 'y' or answer == 'Y':
total=0
# print(goods)显示goods里的商品
for i in goods:
print(i)
# total+=i[1]
total=total+i[1]
print('总金额:'+str(total))
print('谢谢您的光临!!!')
break
else:
continue
elif int(choice) in (range(0, len(products))):
goods.append(products[int(choice)])
print(str(products[int(choice)][0]), products[int(choice)][1])
else:
print("商品不存在!")
except:
print("请输入整数数字!")
改动之前:改动原因(没思考清楚结构,导致后续可能继续报错)