python 小案例demo06

products = [["iphone",6888],["MacPro",14800],["小米6",2499],["Coffee",31],["Book",60],["Nike",699]]
print("-"*5,"\t商品列表","-"*5)
for i,x in enumerate(products):
print(i,end="\t")
print(x[0],end="\t")
print(x[1])
#购物列表 定义购物车
shoppling_list = []
sum_money = 0
#不断咨询客户要购买的商品编码
while 1:
user = input("请输入您要购买的商品编码 q为退出")
if user != "q" and 0 <= int(user) <len(products):
user = int(user)
shoppling_list.append(products[user][0])
sum_money += products[user][1]
# 咨询是否继续购买
print("是否继续购买 q为退出")
elif user.__eq__("q"):
print("-"*5,"\t取消购物成功\t","-"*5)
print("您买了:")
# 遍历展示购物车清单
for shop in shoppling_list:
print("%s"%shop,end="\t")
print("\n总共花费了%d元"%sum_money)
break
else:
print("输入错误,请重新输入:")
continue
posted @ 2020-10-28 12:46  AxeBurner  阅读(119)  评论(0编辑  收藏  举报