Home > Back-end >  while loop in command prompt python
while loop in command prompt python

Time:09-01

How do I create a while loop using python in the command prompt? I want to create an infinite loop that prints a string. I know how to do this normally but being limited to a one line execute makes this very confusing.

CodePudding user response:

$ python -c "while True: print(\"test\")"

Something like this?

CodePudding user response:

The command line won't execute your line if there is another line that's meant to be there. You can just write your loop as you would normally and Python won't start executing it until your loop is closed:

>>> while True:
...    print("abc")
...    #something else
...

You have to press Enter twice to signal Python that you are done with your loop.

CodePudding user response:

The syntax is the same that you would use in an IDE like vs code.

This is a really easy example:

c = 0
while c <= 3:
    print(c)
    c =1

CodePudding user response:

If you're making an infinite loop that simply just prints a string over and over again, something like this should work:

while True: print("Hello!")

Fits on one line and the condition and print argument can be modified to anything you like

  • Related