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

Student
  • 25
  • 1
  • 9
  • You need to implement Multi Threading. – Swakeert Jain Aug 30 '16 at 15:26
  • What is that? def functions? – Student Aug 30 '16 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. – FamousJameous Aug 30 '16 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 – roganjosh Aug 30 '16 at 15:28

3 Answers3

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."
Danielle M.
  • 3,607
  • 1
  • 14
  • 31
  • 1
    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 ;) – Danielle M. Aug 30 '16 at 15:35
  • I' m using python 3.5.1, and I have it working now. Thanks for you help – Student Aug 30 '16 at 15:41
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.

Dmitry Torba
  • 3,004
  • 1
  • 14
  • 24
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)
Amin Etesamian
  • 3,363
  • 5
  • 27
  • 50