python - 预期出现缩进 block python 错误

标签 python indentation python-3.6

我是Python新手,所以对它了解不多。但我知道缩进的基础知识,但无法解决以下代码中的 expected an indented block 错误。

meal =["egg","milk","spam","tomato"]

for item in meal:
    if item =="spam":
        nasty_food =item
        break

if nasty_food:
   print("there is nasty food")

最佳答案

必须一致性缩进代码,因为这是 Python 中必须做的事情。

不要为每个函数的缩进使用不同数量的空格。这将导致您遇到新问题。

试试这个代码:

meal = ["egg", "milk", "spam", "tomato"]

for item in meal:
    if item == "spam":
        nasty_food = item
        break

if nasty_food:
    print("there is nasty food")

关于python - 预期出现缩进 block python 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48722717/

相关文章:

python - 如何从多个子文件夹中随机选择文件

python - 如何根据这些行值从一列中选择 pandas 中的行值,这些值在出现的任何地方都满足另一列中的某些条件

vim - Vim 中 Tab 键 == 4 个空格并在大括号后自动缩进

c# - Visual Studio 2013 缩进在编写时不起作用

php - codesniffer 使用 pear 标准忽略行缩进

python - 字典为相同的键添加值

python - 属性错误 : 'str' object has no attribute 'strftime' when modifying pandas dataframe

c++ - Blas 看起来很慢

python - 检查一个项目是否包含在 ManyToManyField (django)

python - 正则表达式选择除方括号 [] 中的分号之外的每个分号