-3

I’m trying to write code. I’m very new to Python, and i’m using an interpreter on Visual Studio Code. I’m trying to write multiple lines of code, but the \ isn’t working, or i’m doing it wrong. Example:

print(‘hi’) \
print(‘hello’) \

I would expect it to work, but it comes up as syntax error the next time I press enter. Is that because I’m doing it wrong? Otherwise, how would I enter many lines of code. Keep in mind i’m using an interpreter.

teddy
  • 1
  • 4
    What behavior are you trying to accomplish? –  May 13 '20 at 22:26
  • A backslash at the end of a line makes python think that the newline after it doesn't exist. So it would interpret as `print('hi') print('hello') \\`. First off, having two print statements like that is not allowed. Also, a backslash followed by an EOF isn't permitted either. – Eric Jin May 14 '20 at 00:43

1 Answers1

0

Separate your statements using a semicolon(;), if you want multiple "simple statements" on one line. If you want to know more about simple statements, check out the documentation:

https://docs.python.org/3/reference/simple_stmts.html#simple-statements

The backslash(\) is used in the case of logical lines i.e expressions. For example:

5 + 3 \
* 4 - 6

This would work fine because it is an 'expression' and returns a value. The same doesn't work for 'statements'.

This link, I hope, will clear your doubts on the matter:

https://docs.python.org/3/reference/lexical_analysis.html#explicit-line-joining

Zunayn
  • 132
  • 8