简体   繁体   English

Python预计会出现缩进块错误

[英]Python Expected an indented block error

I have just started to learn python. 我刚开始学习python。 On my very first encounter with python I am stuck with the the problem. 在我第一次遇到python时,我遇到了问题。 I am learning to define A function. 我正在学习定义一个函数。 In a simple command I am getting an expected an Indented block error. 在一个简单的命令中,我得到一个预期的缩进块错误。 Please advise. 请指教。 I am using this syntax 我正在使用这种语法

>>>def hello():
...print "Hello"

but when I press the enter key after "Hello" I get the expected an indented block error Actually I have to define this function as following 但是当我在“Hello”之后按下回车键时,我得到预期的缩进块错误实际上我必须将此函数定义如下

>>> def hello():
print "Hello"
print "Computers are Fun"

but I am nowhere getting near to this. 但我无处可去。 please help what is I am doing wrong 请帮助我做错了什么

Check your indentation. 检查你的缩进。 Unlike C, Java, C++, there are no {} to enclose a function, code segment. 与C,Java,C ++不同,没有{}包含函数,代码段。 Indentation is what defines a code section, loop, function etc. 缩进是定义代码段,循环,函数等的原因。

>>> def hello():
...     print "hello"
...
>>> hello()
hello
>>>

Note the indentation from the def line and print line. 请注意def线和打印线的缩进。 Your code is missing correct formatting. 您的代码缺少正确的格式。

You need to indent (4 spaces recommended ) the commands that go in a function, like this: 您需要缩进( 推荐 4个空格)函数中的命令,如下所示:

>>> def hello():
...     print 'hello'
... 
>>> hello()
hello

You need to properly format your functions. 您需要正确格式化您的功能。 Python has very specific spacing requirements. Python具有非常特定的间距要求。

def hello():
    print "Hello"

´expected an Indented block´ means that your indentation is not correct, try following code: '预期缩进块'表示您的缩进不正确,请尝试以下代码:

def hello():
    print "Hello"

def hello2():
    print "Hello"
    print "Computers are Fun"

hello()
hello2()

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

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