Novice python 3 programmer who was trying to get a population growth model to work. Problem I’m having is stylised below.
In interpretive mode, the following code produces an “invalid syntax” error on the line where it prints:
n = 1
for i in range(10):
n += 1
print(n)
Curiously, wrapping it in a function produces the expected output (11):
def function():
n = 1
for i in range(10):
n += 1
print(n)
function()
What’s going on?
You need to put an additional blank line after the for loop to let it know that the statement is done (this is only necessary for the outermost layer, and only in the interpreter). When the interpreter shows
...instead of>>>, that means it is waiting for more input for that statement (in this case, the entire for loop), and because it only executes a statement once it has been completely read in, you need to explicitly tell it when the statement is done.