Python中打字练习的代码
时间: 2023-11-05 13:10:17 浏览: 78
以下是一个简单的Python打字练习的代码:
```python
import time
def typing_test():
text = "The quick brown fox jumps over the lazy dog"
print("Type the following sentence as fast as you can:\n")
print(text)
input("Press Enter to start...")
start_time = time.time()
user_input = input()
end_time = time.time()
time_taken = end_time - start_time
accuracy = calculate_accuracy(text, user_input)
print(f"Time taken: {time_taken:.2f} seconds")
print(f"Accuracy: {accuracy:.2f}%")
def calculate_accuracy(text, user_input):
correct_chars = 0
for i in range(len(user_input)):
if i >= len(text):
break
if user_input[i] == text[i]:
correct_chars += 1
accuracy = (correct_chars / len(text)) * 100
return accuracy
typing_test()
```
该代码会输出一段文本,要求用户尽快输入该文本并计算时间和准确率。用户输入后,程序会计算时间和准确率,并输出结果。
阅读全文