Raw Input with a time limit [duplicate] - python

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.

Related

How to execute a function at the nth second of every minute? [duplicate]

This question already has answers here:
Repeat python function at every system clock minute
(3 answers)
Closed 8 months ago.
time.sleep(60) is oblivious to the time.
So how to get python to execute a function at say the 50-second mark of every minute?
EDIT: I mean I want my function to be executed at:
-20:00:50
-20:01:50
-20:02:50
and so on...
Just put it in a loop:
while True:
my_function()
time.sleep(50)

input in timed while loop [duplicate]

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.

How to move on to next line when an input matches a variable in Python [duplicate]

This question already has answers here:
raw_input without pressing enter
(11 answers)
Closed 4 years ago.
Suppose that x='hello'. My program then asks you for an input. I want it to move on to the next line as soon as you type hello. I'm not sure how to do this without pressing enter.
Though I am not familiar with Python but here is how I would have done it in C#.
Step 1) I will add a OnKeyDown Event to the textbox in which the text is to be added.
Step 2) Every time a key is pressed, my OnKeyDown event will check whether the input matches the particular output or not, in you case, "hello".
Here is how the code will look like:-
private void OnKeyDown(object sender, EventArgs e)
{
if(textbox.Text=="hello")
textbox.Text+="\n";
}
I think the similar logic will work in Python. Try creating or using the similar Event Handler that I used in the code, ie OnKeyDown.

Python: Output while waiting for input [duplicate]

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.

Python loop that always executes at least once? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
do-while loop in Python?
How do I write a loop in Python that will always be executed at least once, with the test being performed after the first iteration? (different to the 20 or so python examples of for and while loops I found through google and python docs)
while True:
#loop body
if (!condition): break
You could try:
def loop_body():
# implicitly return None, which is false-ish, at the end
while loop_body() or condition: pass
But realistically I think I would do it the other way. In practice, you don't really need it that often anyway. (Even less often than you think; try to refactor in other ways.)

Categories

Resources