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.
Related
This question already has answers here:
Python async: Waiting for stdin input while doing other stuff
(3 answers)
Closed 4 months ago.
I want it so that my code continuously replaces itself continuously, and while that executes, I want the input to just take what it is without needing to press enter. I'm an android user and I've already tried downloading non-built-in-modules, so I can't use the keyboard module or anything like that.
My code is basically:
while game == "on":
print("string")
inpu = input
time.sleep(1)
clear screen
Apologies for false text editing or bad "code grammar."
I'm new to coding and stack overflow.
NOT wait for user input while in a loop?
Solution
That is easy, using multi-thread or multi-process. i.e create another thread that handles the input separately.
Demo of full code
Here is a very simple demo of multi-thread to begin.
import _thread
args = None
# Define a function for the thread
def mythread(threadname):
global args
args = input("input anything: ")
# Create one threads as follows
try:
_thread.start_new_thread( mythread, ("UI",) )
while args==None: # mimic your work loop
pass;
print("main loops end demo end")
except:
print ("Error: unable to start thread")
To run
$ python3 demo.py
Output
input anything: hello
main loops end demo end
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!')
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 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.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
raw_input and timeout
I was wondering how I can make my raw_input have a time limit.
I want something like raw_input=("What do you want?")
and then if an input isn't given in less than 5 seconds, it'd print "too late"
I've been trying to read up, but it doesn't seem like anything like this is available for raw_input
Use a timer object, setting it to the required number of seconds; and just use tje timer function to display the message you want once the event object is invoked.