+ 2
Python time library
How can I set the timer for my game(quize). Actually I want to set 5 seconds for user to tell me the input and if user didn't send an input during 5 seconds I will print gameover. And also if user send me the input in 5 seconds I will print(ok) if the answer is correct. My problem now is that I can't make the timer to check if the user enters the input in less than 5 seconds. it would always check it after get input or before that and the rule is that using time library!!
4 Respostas
+ 4
Can you share your code?
+ 4
Fateme Biglari I believe what you are looking for is
import time
time.sleep(5.0) #set for 5 seconds
+ 1
Fateme Biglari
You can use time.sleep() method like this
import time
time.sleep(1) #pause for 1 sec
print('printed after 1sec')
But it will pause everything so you can use time.time to achieve same and also you can do something without any pause.
from time import time
cooldown = 5
start = time()
while True:
if (time()-start) >= cooldown:
print(f"{cooldown} second reached")
break
print(f"untill {cooldown} second do something")