Indentation in python

hello. i am a total noob. new to coding, new to python. i write code in the jedit text editor i get indentation errors on some of my code but i can’t figure it out. the indentations in jedit are correct. now here’s the kicker. sometimes the code works and sometimes it doesn’t. this is driving me crazy!!! the program is a financial calculator that uses the basic formula interest = principle * rate * time. i write code in the main function like so and it works:

#main definition
def main():
	print('Welcome to the financial calculator')
	
#function calls

#call to main
main()

testing this code i get the expected output. as soon as i add a second function

#enter_data definition
def enter_data():```

i get an indentation error on the main function definition, yet as you can see the indentation is correct at 4 spaces.  i have it setup automatically in jedit.  sometimes i get the error message adding if blocks to main() or enter_data().  please help before i loose it.

Your broken code example wasn’t posted properly, so we can’t see it. Please try copying and pasting the entire ‘broken’ version of your code in between the ‘````’ markers, and don’t put anything else between then.

Hi Joe,

We’re more than happy to help you, but you have to help us to help you.

We’re not mind-readers and can’t guess what the problem is without seeing either your code or the full error message :slight_smile:

Well, actually we can guess, but its not always very accurate :slight_smile:

My guess is that you have code that looks like this:

def enter_data():

def main():
    print('Welcome to the financial calculator')


main()

Am I close? The problem is that the enter_data function has no code inside it, and the interpreter thinks that def main is meant to be inside enter_data. Yes, you can put functions inside other functions!

So my guess is that Python is complaining that def main is not indented, probably something like:

IndentationError: expected an indented block after function definition

Am I close?

If that is your error, you can fix it by putting a temporary pass inside the enter_data function (don’t forget to indent it!) until you are ready to put real code there.

def enter_data():
    pass

def main():
    print('Welcome to the financial calculator')


main()

Now the enter_data function has a body, and the interpreter is satisfied.

If my guess was wrong, please post more information!

Please copy and paste your code as text into a post. If you are replying by email, put your code between “code fences”:

```
# code goes here
```

Notice that the three backticks go on their own lines.

If you are using the Discourse web forum to post, you can use the </>
button in the text widget.

If your code is less than, say, 30 or 40 lines, just copy and paste the lot. If it is more than 30, it is a good idea to try to clean it up a bit to the smallest amount of code that still fails.

Then do the same thing for the full error message you get. Don’t
summarise it or abbreviate it, copy and paste it. (Also put it within
the code fences or </> tags.)

1 Like

One thing your editor may be complaining about, unintuitively: It’s typical to leave a space after the comment marker, when adding comments to your code. So, this:

#main definition
def main():
    print('Welcome to the financial calculator')

Should instead look like this:

# main definition
def main():
    print('Welcome to the financial calculator')

It’s subtle, but it can cause issues. (Not with running the code, though, only with how your editor views the code.)

Beyond that, we don’t know what’s actually contained here:

#function calls

But whatever it is, if it’s supposed to be part of the main() function then it should also be indented four spaces. If anything is NOT indented four spaces (including that comment), then you’ve ended the def main(): block and are now outside it. So, this:

# main definition
def main():
    print('Welcome to the financial calculator')

#function calls

is very different from THIS:

# main definition
def main():
    print('Welcome to the financial calculator')

    # function calls

That may be what’s tripping you up.

I have no idea where you got the idea that Python functions have to
be defined in any particular order. To turn your example around:

>>> def main():
...     enter_data()
...
>>> def enter_data():
...     print("this works")
...
>>> main()
this works

It’s true that you can’t call a function until it’s defined, but
referencing a function from within another function definition is
not the same thing as calling it.

1 Like

@fungi

Crap, you’re right, thanks. As you say, you can’t call a function until it’s defined, but using it in a function definition isn’t the same as calling it. (I’ve deleted the comment @fungi was referring to, to avoid sowing confusion.)

Thanks to all of you for the responses. My problem was that i had a function declaration with no function call in main(). i need to remember to put everything in the code in one shot like you should put opening and closing quotes around a string or opening and closing bracket in C. thanks again.