Issue
I was trying some stuff out in the python interpreter [3.10] and wanted to execute a for loop:
If I execute the following code on python:
>>> for i in range(4):
... print(i, end = ': ')
... print('\n')
I get an error:
File "<stdin>", line 3
print('\n')
^^^^^
SyntaxError: invalid syntax
However if I execute the same code in ipython, there are no errors:
In [1]: for i in range(4):
...: print(i, end = ': ')
...: print('\n')
Output:
0: 1: 2: 3:
Solution
You must only give the python interpreter one statement at a time.
This might be a code block, in which case it will wait for you to dedent before executing.
But including the second statement print('\n')
is invalid input.
On the other hand, in a Python program, executed from a file, you can have multiple statements. This accounts for the difference in behavior.
In short: provide an extra return before print('\n')
so the interpreter can execute your first statement before you move on to the next.
Answered By - BadZen
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.