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

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

VS Code don't read break in if statement

问题

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

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

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

  1. from contents import pantry, recipes
  2. def add_shopping_item(data: dict, item: str, amount: int) -> None:
  3. """add_shopping_item 将包含'item'和'amount'的元组添加到'data'字典中。"""
  4. # 如果item在data中:
  5. # data[item] += amount
  6. # 否则:
  7. # data[item] = amount
  8. data[item] = data.setdefault(item, 0) + amount
  9. display_dict = {}
  10. for index, key in enumerate(recipes):
  11. display_dict[str(index + 1)] = key
  12. shopping_list = {}
  13. while True:
  14. # 显示我们知道如何烹饪的菜谱菜单
  15. print("请选择您的食谱")
  16. print("-------------------------")
  17. for key, value in display_dict.items():
  18. print(f"{key} - {value}")
  19. choice = input(": ")
  20. if choice == '0':
  21. break
  22. elif choice in display_dict:
  23. selected_item = display_dict[choice]
  24. print(f"您已选择{selected_item}")
  25. print("正在检查食材...")
  26. ingredients = recipes[selected_item]
  27. print(ingredients)
  28. for food_item, required_quantity in ingredients.items():
  29. quantity_in_pantry = pantry.get(food_item, 0)
  30. if required_quantity <= quantity_in_pantry:
  31. print(f"\t{food_item} OK.")
  32. else:
  33. quantity_to_buy = required_quantity - quantity_in_pantry
  34. print(f"\t您需要购买{quantity_to_buy}{food_item}")
  35. add_shopping_item(shopping_list, food_item, quantity_to_buy)
  36. for item, quantity in shopping_list.items():
  37. 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.

  1. from contents import pantry, recipes
  2. def add_shopping_item(data: dict, item: str, amount: int) -&gt; None:
  3. &quot;&quot;&quot;add_shopping_item Add a tuple containing `item` and
  4. `amount` to the `data` dict.
  5. &quot;&quot;&quot;
  6. # if item in data:
  7. # data[item] += amount
  8. # else:
  9. # data[item] = amount
  10. data[item] = data.setdefault(item, 0) + amount
  11. display_dict = {}
  12. for index,key in enumerate(recipes):
  13. display_dict[str(index + 1)] = key
  14. shopping_list = {}
  15. while True:
  16. #Display a menu of the recipes we know how to cook
  17. print(&quot;Please choose your recipe&quot;)
  18. print(&quot;-------------------------&quot;)
  19. for key, value in display_dict.items():
  20. print(f&quot;{key} - {value}&quot;)
  21. choice = input(&quot;: &quot;)
  22. if choice == 0:
  23. break
  24. elif choice in display_dict:
  25. selected_item = display_dict[choice]
  26. print(f&quot;You have selected {selected_item}&quot;)
  27. print(&quot;Checking ingredients...&quot;)
  28. ingredients = recipes[selected_item]
  29. print(ingredients)
  30. for food_item, required_quantity in ingredients.items():
  31. quantity_in_pantry = pantry.get(food_item, 0)
  32. if required_quantity &lt;= quantity_in_pantry:
  33. print(f&quot;\t{food_item} OK.&quot;)
  34. else:
  35. quantity_to_buy = required_quantity - quantity_in_pantry
  36. print(f&quot;\tYou need to buy {quantity_to_buy} of {food_item}&quot;)
  37. add_shopping_item(shopping_list, food_item, quantity_to_buy)
  38. for things in shopping_list.items():
  39. 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 进行比较。任选其一:

  1. if int(choice) == 0:
  2. break

或者

  1. if choice == "0":
  2. 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;

  1. if int(choice) == 0:
  2. break

Or

  1. if choice == &quot;0&quot;:
  2. 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:

确定