英文:
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) -> None:
"""add_shopping_item Add a tuple containing `item` and
`amount` to the `data` dict.
"""
# 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("Please choose your recipe")
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"You have selected {selected_item}")
print("Checking ingredients...")
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"\tYou need to buy {quantity_to_buy} of {food_item}")
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
根据 DarkKnight 和 Matthias 的评论;您应该使用第二个选项,因为您可以输入无法转换为 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 == "0":
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论