Lets say:
import.time
print('Make a guess: ')
time.sleep(0.5)
guess = input()
if guess == 45:
print('Correct')
I only want this to work if 45 is written in less than 4 seconds. How do I do that?
Lets say:
import.time
print('Make a guess: ')
time.sleep(0.5)
guess = input()
if guess == 45:
print('Correct')
I only want this to work if 45 is written in less than 4 seconds. How do I do that?
The simplest thing you can do is to keep track of the time spent:
import time
start = time.time()
guess = input()
end = time.time()
if end-start > 4:
print('Sorry, you took too long!')
elif guess == '45':
print("Hooray! You're right!")
else:
print('Nope, sorry.')
note: I also changed 45
to '45'
, because input
returns a string in Python3. If you're using Python2, you should use guess = raw_input()
instead.