>>> a = 5
>>> if a == 5:
... print("a is 5")
... else:
... print("a is not 5")
... print("if-else end")
猜看看在 python interactive mode 下這樣對不對?
答案是會跳出 SyntaxError: invalid syntax
錯誤
The body of the loop is indented: indentation is Python’s way of grouping statements. At the interactive prompt, you have to type a tab or space(s) for each indented line. In practice you will prepare more complicated input for Python with a text editor; all decent text editors have an auto-indent facility. When a compound statement is entered interactively, it must be followed by a blank line to indicate completion (since the parser cannot guess when you have typed the last line). Note that each line within a basic block must be indented by the same amount.
有清楚指出在 interactive mode 一定要用空白行作為最後一個區塊的結束
但這只有限制在最後一個區塊,所以多用函數包住,就可以讓 print 直接在 if 結束之後使用,但函數結束後還是只能空白行結束;以及限制在interactive mode,所以在 jupyter notebook 或者寫成 python 檔案,上面的程式碼都不會出錯。