仅在按下键时启动计时器(表示用户已开始输入)
Starting timer only when a key is hit (indicating that user has started typing)
我正在尝试制作打字速度测试仪。该程序从包含单词的文件中取出一行并将其打印在控制台中,然后它要求用户输入打印的行以检查用户输入的速度。但是我只想在用户按下第一个键时启动定时器功能,然后在他输入整行后按回车键时结束它。
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 now\n\n" + 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)
我正在尝试制作打字速度测试仪。该程序从包含单词的文件中取出一行并将其打印在控制台中,然后它要求用户输入打印的行以检查用户输入的速度。但是我只想在用户按下第一个键时启动定时器功能,然后在他输入整行后按回车键时结束它。
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 now\n\n" + 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)