statement that executing code over and over [duplicate] - python

This question already has answers here:
How to run a script forever? [duplicate]
(8 answers)
Closed 3 years ago.
I do not know how to do code that is executing over and over again. I would like to achieve something like this:
(< stands for input, > stands for output)
message=input()
print('Write down the text.')
>Write down the text.
<qwerty
>qwerty
>Write down the text.
<asd
>asd

You can achieve this with a
while True:
// code you want to execute repeatedly here
The while loop will continue to execute until the condition becomes false (which it never will, in this case) so if/when you want to break out of the loop you'll need to have use a break statement

Do you mean like this?
while True:
message=input("Write down the text: ")
print(message)

Related

Else in For Loop example [duplicate]

This question already has answers here:
Why does python use 'else' after for and while loops?
(24 answers)
Closed 2 years ago.
In the image attached, when i=3, why does the "break" take us to the final print("Out side the loop")? Why doesn't it jump to the part:
else:
print ("Loop terminates with success")?
I am struggling to understand this.
for...else loops only run the else when the loop terminates normally and a break is not encountered. So, it skips the else since the loop was ended early.

Adding error check gets script stuck in infinate loop (python) [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
What's the canonical way to check for type in Python?
(15 answers)
Closed 2 years ago.
I'm working on a script to take user input and write it into an Excel file
currently the script works but when trying to add in logic to reject non-integer answers, one of two things happen:
it gets stuck in an infinite loop after properly rejecting the first answer where it keeps asking for a new answer and never accepts it
it properly rejects the first answers, but then accepts the second no matter what it is
this is the code being used where it gets stuck in an infinite loop (I have tried a few other variations that do the same thing but this is the most recent)
true = True
x = input('What is the first marked distance?')
while true == True:
if x == int:
cl.value = x
break
else:
x = input('Please enter a whole number')
any advice on why this is looping and not working would be greatly appreciated
Use float(x)%1 == 0 instead of x == int

Is there an easy way to restart a script if the user inputs invalid data [duplicate]

This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 6 years ago.
Basically, I have a "raw_input" then an "if" statement and then many "elif" statements after it. If the user inputs invalid data I want the "else" to print a string, then restart the program so the user can try again. How can I do this?
Put your code in a loop:
while True:
# Code here
To exit the loop, use break, which will also quit the program.
while True:
# Code here
if running == False: # The condition for breaking is up to you, if you're using one
break

Read python shell [duplicate]

This question already has answers here:
Taking input from sys.stdin, non-blocking
(7 answers)
Closed 6 years ago.
Is there a way to check if there is anything entered in the shell without using raw_input?
For example instead of having something like question = raw_input("Enter Q to quit") I just want to read if anything is entered into the shell, "Q" and quit.
The reason I need this is because I don't want to block my program from executing with the raw_input. I need it to periodically check at any given point if "Q" is entered at all.
Something like this might work
import sys
data_input = sys.stdin.readlines()
print "Counted", len(data_input), "lines."

How can I show never ending dots ("...") in a while loop [duplicate]

This question already has answers here:
Loading animation in python
(3 answers)
Closed 7 years ago.
Is it possible to show something like this in Python?
Checking.
Checking..
Checking...
Checking.
I want it to just show one line of this while the script is running, but stop when it's done. The script I want to add it to is here: https://github.com/brandonusher/Python-Scripts/blob/master/check_port.py
Output the text with no newline character, then flush stdout. Output a \r to make the cursor go back to the beginning of the line. Repeat until done. Don't forget to overwrite existing text.
while True:
sys.stdout.write('\rfoo. ')
sys.stdout.flush()
delay(100)
sys.stdout.write('\rfoo.. ')
sys.stdout.flush()
delay(100)
sys.stdout.write('\rfoo...')
sys.stdout.flush()
delay(100)

Categories

Resources