中断循环而无需添加终止while循环的字符串

时间:2019-02-23 14:07:48

标签: python python-3.x

因此,我试图在不添加字符串(“停止”)且不必输入停用词数量的情况下中断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)

1 个答案:

答案 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)