简体   繁体   中英

How to implement threading into this? - typing speed tester

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} !')

The reason your loop doesn't stop is that you check if the time is elapsed outside of the sentence loop. The following code should work:

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 = 10
    game_active = True
    currenttime = datetime.datetime.now()
    endtime = currenttime + datetime.timedelta(seconds=x)
    
    while game_active:
        for word in sentence_to_match:
            print(word)
            matching_text = input()
            now_time = datetime.datetime.now()
            if now_time >= endtime:
                print('Time is up!')
                game_active = False
                break
            else:
                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 {60 * len(correct) / x} !') # convert wps to wpm

Moreover, this code will not count the last word typed when the time is up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM