How to get out of while loop "…." and continue new line

Question:

How can I end writing while loop and continue a new line while writing the code in the terminal?

>>> while True:
...  print('W')
...  
Asked By: ayse

||

Answers:

Hit enter one more time. A blank line will end the loop.

>>> while True:
...  print('W')
... 
W
W
W
W
W
<snip>
Answered By: John Kugelman

You can’t end writing the while loop without executing the while loop. In your example, hitting return once more will cause the while loop to execute and print an infinite stream of "W" + newline.

If you want to write more code in the terminal without executing the while loop immediately, you need to define a function. Then you can type out the entire function definition without executing any of the code. Hit return one more time to complete the function definition. The prompt will change from "…" back to ">>>". Then you can call your new function to execute the code.

>>> def infinite_loop():
...     while True:
...         print("W")
...     print("this line will never print")
...
>>> infinite_loop()
Answered By: b3nparker
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.