0

I am trying to write a program that will measure a user's typing speed however I cannot get the loop where the user inputs to terminate when the time is up. I want the loop to stop as soon as the time is finished.

from datetime import datetime, timedelta
text = 'Hello My name is matthew'

def some_function(duration):
    end_time = datetime.now() + timedelta(seconds=duration)
    while datetime.now() < end_time:
       answer = input('Type the following: ' + text + '\n\n')

duration = int(input('How long do you want to test yourself: '))

some_function(duration)
Matthew Doig
  • 25
  • 1
  • 4
  • Does [this](https://stackoverflow.com/questions/1335507/keyboard-input-with-timeout) answer your question? – nikeros Nov 08 '21 at 18:34

1 Answers1

0

The problem is that input() is blocking. The rest of the code isn't executed until the user submits their input by pressing enter. You should check out this thread for non-blocking console input