Using for loops in python interpreter

2k views Asked by At

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?

1

There are 1 answers

1
Tkinter Lover On BEST ANSWER

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