I'm trying to count the number of times a person presses a button a certain number of times.
import turtle
if random.randint(1,2) == 2:
turtle.listen()
turtle.onkey(number() ,'s')
else:
pass
def number():
global shots
shots += 1
shots was declared earlier.
That's what I've done but I need to set some type of time limit, so the user can only press it for say 4 seconds then if shots are bigger than a number it's do something.
Is there any way to do this, thank you
You can use the
turtle.ontimer
function to implement a timer. In thetimer
function I increment the global time variable and callturtle.ontimer(timer, t=100)
which automatically callstimer
again after the specified timert
.