Skip to content Skip to sidebar Skip to footer

Python Unexpected Indentaton Error Main()

I have no idea how to fix this. I've tried retyping the program. I get an unexpected indentation error for the last main function. resident = 81 nonresident = 162 def main():

Solution 1:

You don't have an except clause to match the try.

Solution 2:

Despite what everyone else is saying, if you have a try, you don't have to have an except: you must have an exceptor a finally.

That may seem nitpicky, but it's pretty plausible that the code you were copying was actually using finally (e.g., to make sure some cleanup code always gets run—given that it's doing C-/Java-style manual cleanup).

At any rate, adding an except clause is not the right answer. If you're actually looking to handle or ignore errors, then yes, add an except clause. If you're looking for somewhere to add cleanup code that gets run even on an error, add a finally clause instead. If you don't want either, just remove the try line.

Solution 3:

you are mixing room type and code type in your if else statement. You are also missing your except statement. This should be your last two lines before calling main.

It should read:

except IOError:
    print('an error occurred trying to open or read enroll.txt')

Solution 4:

You are using the try statement. It means that you MUST have an except block

try:
    some code
except:
    pass

It is simplification, but will solve your problem.

Solution 5:

The other answers have (correctly) pointed out that you need to have an except to go with your try: (or not use a try altogether). I'm going to try to explain how you could go about debugging similar issues in the future, since these can be very annoying to debug.

Your error message most likely looked something like this:

  File "test.py", line 74
    main()
    ^
IndentationError: unexpected unindent

At first glance, that doesn't seem too helpful, and it's definitely not very clear.

The fact that you "tried retyping the program" sounds like you saw IndentationError and thought, "this means my indents are messed up; maybe there's an extra space somewhere or a mixture of spaces and tabs or something." Those are definitely valid IndentationErrors, and retyping the program would fix them, if you didn't make the same typing error again. (On a side note, I believe that any text editor worth using will have the option to convert all of your tabs to 4 spaces to avoid a lot of indent headaches.)

However, looking at the error text, we see "unexpected unindent". To me this sounds kind of like gibberish, but what it means is that, when python was looking at your program, it ran into a line that it thought should be indented more than it was. (Unfortunately, this same kind of error can be called "unexpected unindent" or "expected an indented block", and it can even show up as a plain old SyntaxError: invalid syntax.)

So knowing what the error message means, we can look back through the code to see why python thought that main() was indented strangely- though we have to be careful because python isn't always right about where the problem actually is. Here would be something like my thought process:

  • There's a function definition right before main() is called, maybe that function wants main() to be inside it? No that isn't it, because python only really expects at least one line of code after a function definition.
  • What else "wants" code to be indented after it? if, elif, and while all do, but all of them have code after them as well.
  • The only other "indenter" is try:. At this point, you either know that try:needs to be followed by a except: or a finally: and you fix it, or you don't. If you don't know that, than you could still see that try: is followed by an indented block, and, given that it is a likely culprit for you error, decide that it's time to look up what try: does.

Well I hope that helps troubleshooting problems like this in the future, and check out abarnert's answer and my link for more on handling errors with try/except/else/finally statements.

Post a Comment for "Python Unexpected Indentaton Error Main()"