简体   繁体   English

预计会出现缩进块 python 错误

[英]expected an indented block python error

I am new to Python so don't know much about it.我是 Python 新手,所以不太了解它。 But I know the basics of indentation and I cannot solve the error of expected an indented block in the following code.但是我知道缩进的基础知识,我无法解决以下代码中预期的缩进块的错误。

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

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

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

You have to indent code with consistency since that is a must do in Python.必须一致地缩进代码,因为这是 Python 中必须做的。

Don't use different number of spaces for indent per function.不要为每个函数使用不同数量的空格进行缩进。 This will lead you to new issues.这将导致您遇到新问题。

Try this code:试试这个代码:

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

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

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

Visually I don't see an indentation issue in the code you posted.从视觉上看,我在您发布的代码中没有看到缩进问题。 Verify that you are not mixing up tabs and spaces as that can also lead to indentation issues.确认您没有混淆制表符和空格,因为这也会导致缩进问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM