I need a function to start a countdown from 3 for a rock, paper, scissors game which uses the webcam, I can't use time.sleep() as the script reads the input from the camera and then compares it with the computer's choice without stopping and time.sleep() stops the script from running
Currently, my function counts up from zero, and also prints multiple iterations of the same number.
I also feel like the function is overcomplicated, are there any simpler methods?
Code:
def countdown():
countdown_time = 3
start_time = time.time()
while (time.time() - start_time) < countdown_time:
print(round(time.time() - start_time))
if (time.time() - start_time) >= countdown_time:
break
Ideally it should just print:
3
2
1
But it prints something more like this:
1
1
1
2
2
2
3
3
3
I think this what you are looking for:
from time import sleep
def countDown():
for i in range(3, 0, -1):
print(i)
sleep(1)
countDown()
You are saying that you can't use the time.sleep()
as that particular function won't start the program which is wrong. It works here just fine.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.