0

I'm trying to start a for loop after doing something else in python interpreter on the same line, and it throws a SyntaxError when I do so.

>>> a,b = 0, 1;\
... for i in range(1, 10):
  File "<stdin>", line 2
    for i in range(1, 10):
      ^
SyntaxError: invalid syntax

Of course I could just execute them separately here, but if I want to have this inside a function definition then I can't exactly do that. What is the correct syntax to do it in the interpreter?

Stidgeon
  • 2,673
  • 8
  • 20
  • 28
syzygy350
  • 23
  • 5
  • 1
    I'm confused -- if you're using the semicolon to denote the end of a statement, isn't the point of that to continue the input on the same line? – Aelarion Apr 29 '21 at 02:53
  • 1
    *"if I want to have this inside a function definition then I can't exactly do that"* why not? – wim Apr 29 '21 at 02:54
  • 1
    What's with the semicolon and backslash? –  Apr 29 '21 at 03:13

1 Answers1

0

When you have a backslash, you are telling it to ignore the new line. So Python thought your code was a,b = 0, 1 for i in range(1,10):. This is clearly invalid syntax. So, you must remove the semicolon and the backslash. When you want to go to the new line, use shift + enter key.

After that, it should work:

enter image description here

Tkinter Lover
  • 835
  • 3
  • 19
  • Right, I thought that I'd have to demarcate each line end when defining a function with this in it, but it just stays "in" the function until I press enter twice anyways... – syzygy350 Apr 29 '21 at 04:32