0

I have created a MWE:

x = True
if x:
    print('111111')
y = 1

When running in python interactive mode, I get

>>> x = True
>>> if x:
...     print('111111')
... y = 1
  File "<stdin>", line 3
    y = 1
    ^
SyntaxError: invalid syntax
>>>

The code looks very correct. If I add them line by line, they will be fine but now when copying them all together.

What is wrong?

mercury
  • 189
  • 1
  • 15
  • 1
    In Python's interactive mode, you have to end all multi-line statements (such as the one starting `if x:`) with a blank line, before you enter another top-level statement. – jasonharper Jan 25 '22 at 04:24
  • dupe of dupe of ... https://stackoverflow.com/q/46805254/674039 – wim Jan 25 '22 at 04:29

3 Answers3

1

You have to put a space after the final line of an indented block in Python terminal, to indicate the end of the indentation.

So just hit enter again before writing y = 1.

csjh
  • 154
  • 2
  • 11
1

The interpreter is saying that the indentation of the second statement y=1 was unexpected.You should have entered a blank line to end the first statement (i.e., "if") , before you start writing the next statements(i.e y=1).

>>> x=True
>>> if x:
...     print('1111')
...
1111
>>> y=1
A. Faiyaz
  • 46
  • 3
0

you have to enter a new line after the print statement two times. Then only it exits the if statement