0

Code:

Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> for i in range(3):
...     print(1)
... print(2)
  File "<stdin>", line 3
    print(2)
        ^
SyntaxError: invalid syntax

Expected output:

1
1
1
2

With several test, I knew python interactive shell return invalid syntax error when the indentation block has lower.

Test code:

if true:
    print("test")
print(test)

output:

File "<stdin>", line 3
    print("test")
        ^
SyntaxError: invalid syntax

To solve this problem, I must using Enter key.

>>> for i in range(3):
...    print(1)
... 
1
1
1
>>> print(2)
2

But why? why I have to separate two code using enter? I want to know the reason.

Note: I'm bad at English and it's hard to describe it. "end of indentation" and "indentation block has lower" means that situation

if true: //no indentation, no tab or space
   print(1) //in if syntax, we have to make indentation 
print(2) //when if end, We don't make indentation. Compare to line 2, 'indentation block has lower' and 'indentation had ended'

I don't think it's right expression, So you may a little confused. if you understand this, please edit this question and make it right.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
jun
  • 33
  • 4

0 Answers0