I am still a newbie when it comes to python coding so please be kind! I have been suggested to use threading to run both the 'timer' and the for/while loop together, but I am not sure if it's necessary or how to implement it at all.
Being a beginner it looks as though threading is still a bit far from my reach at least for now. My goal here is to have an extensive list of random words that the user must input and at the end I'll calculate their WPM and accuracy (I'll expand the list later).
What I don't understand is why the while loop doesn't stop even though the program seems to reach the ''time's up'' part.. how can I fix this code?
import datetime
sentence_to_match = ['hello', 'darkness', 'my', 'old', 'friend', 'fox']
instructions = 'You have 1 minute to type all the words you can'
print(instructions)
start_test = input('Press y to start or n to exit the test : ').lower()
wrong = []
correct = []
total = []
if start_test == 'y':
x = 60
currenttime = datetime.datetime.now()
endtime = currenttime + datetime.timedelta(seconds=x)
print(currenttime)
print(endtime)
while currenttime < endtime:
now_time = datetime.datetime.now()
print(now_time)
if now_time >= endtime:
print('Time is up!')
break
for word in sentence_to_match:
print(word)
matching_text = input()
total.append(word)
if matching_text == word:
correct.append(word)
elif matching_text != word:
wrong.append(word)
print(f'You typed a grand total of {len(total)} words!')
print(f'These are all the words you have spelled correctly : {correct}')
print(f'These are all the words you have spelled wrong : {wrong}')
print(f'This is your WPM {len(total) / 5 / 60} !')