I am new to Python but am following along a book closely. I can’t figure out why this won’t work. When I click return to write a new line below the second “if” statement, the code attempts to run and says invalid syntax, highlighting the second “if”
Advertisement
Answer
The interactive window of IDLE knows that a block will follow an if
statement, and expects that block to be ended by an empty line. According to the image of your code, you only used a backspace, and then IDLE expects an unindented part of the if
instruction, said differently an else
or elsif
.
TL/DR: use an empty line to end an indented bloc.
The interactive window is… interactive! It executes one statement at a time. If you want to prepare a full script and then execute it as a whole, you must create a new file (menu File/New) write the full script and save it to disk at execution time (F5 of menu Run/Run Module).