简体   繁体   English

如何实现线程化? - 打字速度测试仪

[英]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.对于 python 编码,我还是个新手,所以请多关照,有人建议我使用线程来同时运行“计时器”和 for/while 循环。 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).我的目标是获得用户必须输入的大量随机单词列表,最后我将计算他们的 WPM 和准确度(稍后我将扩展列表)。

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?我不明白的是为什么即使程序似乎到达“时间到了”部分,while 循环也不会停止。我该如何修复这段代码?

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.此外,此代码不会计算时间到时输入的最后一个单词。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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