简体   繁体   中英

Python Expected an indented block error

I have just started to learn python. On my very first encounter with python I am stuck with the the problem. 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

>>> 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. 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. Your code is missing correct formatting.

You need to indent (4 spaces recommended ) the commands that go in a function, like this:

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

You need to properly format your functions. Python has very specific spacing requirements.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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