因此,我试图在不添加字符串(“停止”)且不必输入停用词数量的情况下中断while循环。
while True:
try:
product = get_user_input("Input the product name: ")
quantity = get_user_input("Input the product quantity: ", int)
shopping_list.add_item(product, quantity)
if product == "stop":
break
else:
continue
except Exception as e:
print("\nAn error occurred:", e)
答案 0 :(得分:2)
如果product
是"stop"
,则无需提示数量或向购物清单中添加任何东西。早些检查一下。
while True:
try:
product = get_user_input("Input the product name: ")
if product == "stop":
break
quantity = get_user_input("Input the product quantity: ", int)
shopping_list.add_item(product, quantity)
except Exception as e:
print("\nAn error occurred:", e)