Hi,
I keep receiving an unexpected indent error on question 7 of the Python Basics exercise in week 2.
Don’t understand what I am doing wrong, hopefully, somebody can help!
** Moderator Edit: Code Removed**
Thanks,
Hi,
I keep receiving an unexpected indent error on question 7 of the Python Basics exercise in week 2.
Don’t understand what I am doing wrong, hopefully, somebody can help!
** Moderator Edit: Code Removed**
Thanks,
Hi,
You are not returning the value of ‘s’ with the correct indent. Keep the indent right just below the indent of (def) and then run the cell.
I think that’s what is throwing an error. Your codes are fine.
Thanks.
Thanks for replying,
If I do it like that, it gives an outside function error.
If I paste the line directly under # YOUR CODE ENDS HERE, it still returns the error.
Solved it!, thank you
The lines that are incorrect are the three actual code lines of the function. I guess from your later comment you figured that out. Indentation is part of the syntax of python: you can’t just do it the way that looks nice to you: you have to understand and follow the rules.
Note that this section is not teaching you python: you’re already supposed to know python as a prerequisite here. The purpose here is just to introduce you to numpy. If this is literally your first exposure to python, it might be a good idea to go spend some time with online tutorials or even take an “intro to python” course first.
This is of course a correct observation as far as the semantics of the function, but interesting that the syntax error doesn’t show up there. I guess empty functions are allowed? The parser seems to accept a function with zero body and treats the three unindented lines as outside the function, at same scope as the def
statement. Then all of a sudden an indented line without a context for it (function definition, control statement, etc) which causes the unexpected indent exception. Removing the indent solves the unexpected indent problem, but creates another as the return
must occur within a function. @Joost_Smeets found this out while debugging.
See 7. Simple statements — Python 3.12.0 documentation
return
may only occur syntactically nested in a function definition
@paulinpaloalto I think we were editing in parallel
Yes, I’m not sure exactly what is going through the parser’s tiny little mind at this point, but it seems analogous to the Case of the Missing Close Paren: it keeps hopefully burrowing forward looking for that close paren and then “throws” on the next line when it sees some other syntactic element that conflicts with that expectation.
Actually I think it probably considers that the function def was empty and then there are three lines of code at the same scope as the empty function followed by that indented “return” which just makes no sense, since there is no active function body. But it complains about the highest level point first: the fact that there is no reason for the line to be indented, not the fact that it makes no sense to have a return
not in a function body,
@ai_curious: At least it’s good that we both ended up in the same place. Thanks!
Hi Joost,
Glad you’ve got that! Yes, those three lines were actually not at the proper place. I did identify that too but the error was throwing somewhere else and it caught my attention.
Indentation, really is an important part of Python as rightly said by Paul sir. We just can’t choose to write our codes anywhere