0

I'm trying to run a timer inside of a function of my code. I need to start the timer slightly before the user starts typing, then stop the timer when the user has entered the alphabet correctly. Here is my code:

import time
timec = 0
timer = False

print("Type the alphabet as fast as possible.\nYou MUST be accurate!\nYou will be timed")
timer = True
attempt = input("The timer has started!\nType here: ")

while timer == True:
    time.sleep(1)
    timec = timec +1


if attempt == "abcdefghijklmnopqrstuvwxyz":
    timer = False
    print("you completed the alphabet correctly in", timec,"seconds!")
else:
    print("There was a mistake! \nTry again: ")

The issue is that it will not let me enter the alphabet. In previous attempts of this code (Which I do not have) i have been able to enter the alphabet, but the timer would not work. Any help is appreciated

4
  • You need to implement Multi Threading. Commented Aug 30, 2016 at 15:26
  • What is that? def functions? Commented Aug 30, 2016 at 15:27
  • 3
    It might be easier to record the time, prompt the user to start typing then when they hit enter and the input call returns, record the end time. The time taken would be the end minus the start. Commented Aug 30, 2016 at 15:27
  • I think you're over-complicating. Since you only want to know the elapsed time, do something like start = datetime.datetime.now() above the first print and end defined the same way, above if attempt.... Then just subtract to find elapsed time Commented Aug 30, 2016 at 15:28

3 Answers 3

2
import time

start = time.time()
attempt = input("Start typing now: ")
finish = time.time()

if attempt == "abcdefghijklmnopqrstuvwxyz":
    print "Well done, that took you %s seconds.", round(finish-start, 4)
else:
    print "Sorry, there where errors."
Sign up to request clarification or add additional context in comments.

2 Comments

Here's my high score: Start typing now: abcdefghijklmnopqrstuvwxyz Well done, that took you 0.3355 seconds. That's how fast I can hit Paste and Enter ;)
I' m using python 3.5.1, and I have it working now. Thanks for you help
2

Think carefuly about that you are dong

  1. You ask for a user-entered string
  2. While timer equals True, you sleep for one second and increase the count. In this loop, you do not change the timer.

Obviously, once user stopped entering the alphabet and pressed enter, you start an infinite loop. Thus, nothing seems to happen.

As other answers suggested, the best solution would be to save the time right before prompting user to enter the alphabet and compare it to the time after he finished.

Comments

0

you could do something like:

import datetime

alphabet = 'abcdefghijklmnopqrstuvwxyz'

print('Type the alphabet as fast as possible.\nYou MUST be accurate!\nYou will be timed"')
init_time = datetime.datetime.now()
success_time = None

while True:
    user_input = input('The timer has started!\nType here: ')
    if user_input == alphabet:
        success_time = datetime.datetime.now() - init_time
        break
    else:
        continue

print('you did it in %s' % success_time)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.