[Asm] 纯文本查看 复制代码 products = [["iphone", 6888], ["MacPro", 14800], ["小米6", 2499], ["Coffee", 31], ["Book", 60], ["Nike", 699]]
print("-" * 5, "\t""商品列表", "\t", "-" * 5)
for product in products:
print(products.index(product), end="\t")
print(product[0], end="\t")
print(product[1])
shopping_cart = []
sum_money = 0
while True:
user = input("请输入您需要购买的商品编号(q为结账):")
if -1 < int(user) <= len(products):
print("已在您的购物车里添加商品:%s" % products[int(user)][0])
shopping_cart.append(products[int(user)])
sum_money += products[int(user)][1]
# print(shopping_cart)
elif user == "q":
print("您购买的商品有:", end="")
for i in shopping_cart:
print(shopping_cart.index(i), end="\t")
print(i[0], end="\t")
print(i[1])
break
else:
print("您输入的商品编号不存在,请重新输入!")
错误提示:ValueError: invalid literal for int() with base 10: 'Q'单独执行elif也能进行啊,为什么整体运行就提示格式错误呢?前面的input 接收的user 不正是字符串类型吗?这里按q为什么会出错呢?谢谢! |