VS Code 不读取 if 语句中的断点。

huangapple go评论52阅读模式
英文:

VS Code don't read break in if statement

问题

在我的代码中,当输入0时,VSCode不读取if语句中的break,也不跳到最后的for循环。

我的语法是正确的,我进行了调试,但它没有跳出循环。

我进行了调试。
而且我写了"pass"而不是"break",然后缩进了for循环,它就运行了。如何使用"break"来运行它?

from contents import pantry, recipes


def add_shopping_item(data: dict, item: str, amount: int) -> None:
    """add_shopping_item 将包含'item'和'amount'的元组添加到'data'字典中。"""
    # 如果item在data中:
    #     data[item] += amount
    # 否则:
    #     data[item] = amount
    data[item] = data.setdefault(item, 0) + amount


display_dict = {}
for index, key in enumerate(recipes):
    display_dict[str(index + 1)] = key

shopping_list = {}
while True:
    # 显示我们知道如何烹饪的菜谱菜单
    print("请选择您的食谱")
    print("-------------------------")
    for key, value in display_dict.items():
        print(f"{key} - {value}")

    choice = input(": ")

    if choice == '0':
        break
    elif choice in display_dict:
        selected_item = display_dict[choice]
        print(f"您已选择{selected_item}")
        print("正在检查食材...")
        ingredients = recipes[selected_item]
        print(ingredients)
        for food_item, required_quantity in ingredients.items():
            quantity_in_pantry = pantry.get(food_item, 0)
            if required_quantity <= quantity_in_pantry:
                print(f"\t{food_item} OK.")
            else:
                quantity_to_buy = required_quantity - quantity_in_pantry
                print(f"\t您需要购买{quantity_to_buy}{food_item}")
                add_shopping_item(shopping_list, food_item, quantity_to_buy)

for item, quantity in shopping_list.items():
    print(item, quantity)

我进行了调试。
此外,我写了"pass"而不是"break",并且缩进了for循环,它就成功运行了。如何使用"break"来运行它?

英文:

In my code, VSCode don't read break in if statement when entered 0 and don't skip to last for loop.

My syntaxes are right, I debugged but it doesn't break out of the loop.

from contents import pantry, recipes


def add_shopping_item(data: dict, item: str, amount: int) -&gt; None:
    &quot;&quot;&quot;add_shopping_item Add a tuple containing `item` and 
    `amount` to the `data` dict.
    &quot;&quot;&quot;
    # if item in data:
    #     data[item] += amount
    # else:
    #     data[item] = amount
    data[item] = data.setdefault(item, 0) + amount



display_dict = {}
for index,key in enumerate(recipes):
    display_dict[str(index + 1)] = key

shopping_list = {} 
while True:
    #Display a menu of the recipes we know how to cook
    print(&quot;Please choose your recipe&quot;)
    print(&quot;-------------------------&quot;)
    for key, value in display_dict.items():
        print(f&quot;{key} - {value}&quot;)

    choice = input(&quot;: &quot;)

    if choice == 0:
        break
    elif choice in display_dict:
        selected_item = display_dict[choice]
        print(f&quot;You have selected {selected_item}&quot;)
        print(&quot;Checking ingredients...&quot;)
        ingredients = recipes[selected_item]
        print(ingredients)
        for food_item, required_quantity in ingredients.items():
            quantity_in_pantry = pantry.get(food_item, 0)
            if required_quantity &lt;= quantity_in_pantry:
                print(f&quot;\t{food_item} OK.&quot;)
            else:
                 quantity_to_buy = required_quantity - quantity_in_pantry
                 print(f&quot;\tYou need to buy {quantity_to_buy} of {food_item}&quot;) 
                 add_shopping_item(shopping_list, food_item, quantity_to_buy)


for things in shopping_list.items():
    print(things)

I debugged.
Also I write pass instead of break, and intented for loop, it ran then. How can I run it with break ?

答案1

得分: 1

由于 input 的返回类型始终为 string,您必须将其转换为 int 或将其与 string 进行比较。任选其一:

if int(choice) == 0:
    break

或者

if choice == "0":
    break

根据 DarkKnightMatthias 的评论;您应该使用第二个选项,因为您可以输入无法转换为 int 的字符串。这种方法更灵活,因为您可以将输入与预选的 string(如 "exit"、"stop" 等)进行比较。

英文:

Because the returning type of input is always string, you have to convert it to an int or compare it to a string. Either;

if int(choice) == 0:
    break

Or

if choice == &quot;0&quot;:
    break

As per DarkKnight and Matthias's comments; you should use the second option since you can enter a string that couldn't be converted to an int. This approach is more flexible because you can compare the input to a preselected string like "exit", "stop", etc.

huangapple
  • 本文由 发表于 2023年6月19日 20:11:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76506499.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定