This question already has answers here:
How do I wait for a pressed key?
(13 answers)
Closed 2 years ago.
I was making a terminal-type program with python and I was wondering if I could make it print the next line of text after you press enter
e.g
`print('this was a triumph')
#press enter to print the next line
print('Im making a note here, huge success!')`
You could try:
input('this was a triumph')
input('Im making a note here, huge success!')
or:
print('this was a triumph')
input('Press Enter To Continue:')
print('Im making a note here, huge success!')
Related
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 1 year ago.
I would like to restart my python code from the beginning after a given "yes" input from the user, but I can't understand what I'm doing wrong here:
if input("Other questions? ") == 'yes' or 'yeah':
main()
else:
pass
my code is included within the function main().
Thanks for the help!!
You would probably do it with a while loop around the whole thing you want repeated, and as Loic RW said in the comments, just break out of the while loop:
while True:
# do whatever you want
# at the end, you ask your question:
if input("Other questions? ") in ['yes','yeah']:
# this will loop around again
pass
else:
# this will break out of the while loop
break
This question already has answers here:
How to keep a Python script output window open?
(27 answers)
Closed 2 years ago.
I use notepad++ to write my python code, and when I put in this code for a basic calculator:
However, after the input calls are over, it auto-closes the console window, supposedly because it finishes the rest of the calculations extremely quickly, but how do I stop it from closing?`I use notepad++ to write my python code, and when I put in this code for a basic calculator:
num1 = input("Enter a number")
num2 = input("Enter another number")
result = float(num1) + float(num2)
print(result)
However, after the input calls are over, it auto-closes the console window, supposedly because it finishes the rest of the calculations extremely quickly, but how do I stop it from closing?
You can have an input function at the end of the file. Then it'll halt until the user presses enter.
num1 = input("Enter a number")
num2 = input("Enter another number")
result = float(num1) + float(num2)
print(result)
input('Press the Enter key to exit...')
You're completely right, as soon as Python finishes it sees no need to keep the console window open, so the trick is to make it keep doing something. That way it hasn't "finished", and won't close.
If you add
input()
to the last line of your program, Python will wait until you press Enter, therefore keeping the console window open until you're ready :)
This question already has answers here:
Python: How to get input from console while an infinite loop is running?
(3 answers)
Closed 2 years ago.
When I try to allow the user to input a command in a timed while loop (0.1 seconds) it pauses at the input and doesn't get to the time.sleep() until the user enters something. For example:
while True:
[something happens]
userInput = input("Type something")
if userInput = "foo":
[another thing happens]
time.sleep(0.1)
I want the loop to run normally if there is no user input and process the user input if there is without pausing the entire loop.
Calling the input function requires it to wait until the user types something.
This question already has answers here:
How do I terminate a script?
(14 answers)
Closed 3 years ago.
My break function is not working even though it is in the loop. Please, someone, explain in detail, because I'm new to Python.
q1 = input('want a question?: ')
if q1 == 'yes':
print("let's get started!, press enter")
a1 = input()
else:
print('why did you even start me ?!')
break
Here I expect break to stop the program from going any further.
break doesn't stop your program from going any further. It breaks out of a loop.
You may want sys.exit, e.g.
import sys
sys.exit()
This question already has an answer here:
Exiting while loop by pressing enter without blocking. How can I improve this method?
(1 answer)
Closed 5 years ago.
So I currently have a thread that is outputting, and I need a way to handle input while still allowing output from the thread in the meantime.
Currently I have something like this
def thread_func():
while True:
print("Information")
threading.Thread(target=thread_func).start()
while True:
command = raw_input("Enter a command:")
#dostuff with command
My issue now is that my thread isn't printing anything. Or if it is it isn't showing up
Edit:
Found a solution here
Exiting while loop by pressing enter without blocking. How can I improve this method?
You want to use a Queue. New input will go into the back of the queue, and work will be started from the front.