如何实现线程到这个?-打字速度测试仪



我还是一个python编程新手,所以请大家多多指教!我已经建议使用线程运行'定时器'和for/while循环在一起,但我不确定是否有必要或如何实现它在所有。

作为一个初学者,它看起来好像线程仍然有点远,至少现在我的能力。这里我的目标是有一个用户必须输入的随机单词的广泛列表,最后我将计算它们的WPM和准确性(稍后我将扩展该列表)。

我不明白的是为什么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} !')

循环没有停止的原因是您检查了时间是否在句子循环之外经过。下面的代码应该可以工作:

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

此外,当时间到时,此代码将不计算输入的最后一个单词。

编辑:我刚刚使用pynput库编写了一个版本的程序,即使用户键入一个单词,程序也会终止。这样可以避免强行中断input功能。我认为这是你需要的,而不是线程库。下面是代码:

import time
from itertools import cycle
from pynput import keyboard
sentence_to_match = ['hello', 'darkness', 'my', 'old', 'friend', 'fox']
sentence_loop = cycle(sentence_to_match)
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()
user_input = []
user_words = []
wrong = []
correct = []
total = []
def on_press(key):
global user_input

if key == keyboard.Key.backspace: 
# remove last letter from user_input and from printed text
if user_input != []:
user_input.pop()
print('b', end='')
return
elif key == keyboard.Key.space:
# if user pressed space bar
char = ' '
else:
try:
# if user typed a letter
char = key.char
except AttributeError:
# if user typed something else, don't do anything
return

user_input.append(char)
print(char, end='')
listener = keyboard.Listener(on_press=on_press)
if start_test == 'y':
x = 10
start_time = time.time()
end_time = start_time + x

listener.start()

# print the first word
word = next(sentence_loop)
print('r' + 15*' ', end='')
print('r' + word, end=' ')

while time.time() < end_time:
if user_input != [] and user_input[-1] == ' ': # if user typed space
print()
last_user_word = ''.join(user_input[:-1]) # remove the last space
user_words.append(last_user_word)
if last_user_word == word:
correct.append(last_user_word)
else:
wrong.append(last_user_word)
user_input = []

# print the next word and repeat
word = next(sentence_loop)
print('r' + 15*' ', end='')
print('r' + word, end='')

time.sleep(0.1)

print('ndone!n')
listener.stop()
print(f'You typed a grand total of {len(user_words)} 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} !')

我希望这对你有帮助!

最新更新