Skip to content Skip to sidebar Skip to footer

What Does This Error Mean ? "Expected An Indented Block" Python

My code is the following: def value(one,two): if one < two: return two else: return one Every time I try to run it, it gives the following error : IndentationError: expect

Solution 1:

Python uses (requires) indentation to identify blocks in your code. For example:

def value(one,two):
    if one < two: 
        return two
    else:
        return one

You have something along the lines of:

def value(one,two):
if one < two: 
return two
else:
return one

Which yields the error you are seeing.


Solution 2:

As the previous guys said in their answers, you must indent every piece of code that it's inside another.

Why? You may ask.

Good question, glad you asked. See, Python doesn't have curly braces {}, therefore it has to know somehow where a block starts and ends; here comes the indentation. It may see trivial in other languages that do have curly braces, such as Java or C#, it is a must in Python.

For every code, if it's inside another (A function, a loop, an if statement, and so on) it has to be indented 4 spaces (Or a tab key), no more, no less.

Of course, if you have another code inside the already indented one, you just add another 4 spaces, and so on as long as you need to.

As for your code, it should be like this:

def value(one,two):
    if one < two: 
        return two
    else:
        return one

Always remember indentation, it is Python's essence!

Good luck :)


Post a Comment for "What Does This Error Mean ? "Expected An Indented Block" Python"