仅当按键(表示用户已开始键入)时才启动计时器



我正在尝试制作一个打字速度测试仪。该程序从包含单词的文件中提取一行,并将其打印在控制台中,然后要求用户键入打印的行,以检查用户键入的速度。但我只想在用户按下第一个键时启动计时器功能,然后在键入整行后按下回车键时结束计时器功能。

print(line)
start = time()
entered_line = input()
end = time()
total_time += end - start

上面的代码包含在上面的for循环中。作为一名初学者,我所能做的就是在文件中的行打印到屏幕上后立即开始计时,如果用户不立即开始键入,就会导致错误。因此,一个简单易行的解决方案将受到赞赏。谢谢

我认为这可以得到你想要的

import time
from getkey import getkey
line = "here is the line you are trying to type"
print(line)
print("you can start by hitting any key")
print("hit ENTER when you are done")
k = getkey()
start_time = time.time()
your_input = str(k) + input("you can start nownn" + str(k))
end_time = time.time()
print("time elapsed =", end_time-start_time, "seconds")
print("the status of the line you just typed =", line == your_input)

最新更新