Hello Everyone
This is my first time posting my concern on this platform so please excuse my horrendous way of asking questions. Also, I want to thank you all for having a peek at my difficulties.
Currently, I want to create a quiz-like algorithm that would display a countdown timer that ticks down every second while the user can input their answer anytime. Here is a sample of my code:
import time
chance = 2
def countdown(t):
while t > 0:
secs = t % 60
timer = "{:02d}".format(secs)
print(timer, end="\r")
time.sleep(1)
t -= 1
while chance > 0:
print("Type '1'")
countdown(int(10)
answer = input("Enter: ")
if answer == 1:
print("Correct")
break
elif t == 0:
print("Time over")
break
else:
chance = chance - 1
print("Wrong")
Desired result:
Not to exclude, the code doesn't work due to some syntax errors (I am not a good coder D:, sorry). I would be glad to see other great samples of code that have the exact purpose as my intention or some piece of advice to fix this code.
Once again, I would like to thank you for reading through my trouble and I hope you could provide me with some solid solutions to solve this situation.
As you've mentioned, there are several syntax errors in your code, I'll let you figure those out for yourself (hint for one of them: you shouldn't print
a function).
What you want here is two functions to run simultaneously (the timer running in the background while the input functions runs in forefront). So look up how to do threading with python. Useful resource for Timer Threading in Python: https://www.educba.com/python-threading-timer/
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.