Python模拟英文打字练习的判分功能,文件“in.txt”中包含两段文字,第一段为原文,第二段为用户练习打字时输入的文字。编写程序统计打字练习时,大写字母,小写字母,数字字符以及其他字符的正确率,以及总正确率,正确率保留1位小数。如果某类字符的出现次数为0则正确率显示为100.0%
时间: 2024-03-09 18:50:22 浏览: 105
好的,请稍等一下,我来给你写一下Python代码。以下是代码实现:
```python
with open("in.txt", "r") as f:
original_text = f.readline().strip() # 读取原文
input_text = f.readline().strip() # 读取用户输入的文字
total_count = len(original_text) # 统计总字符数
correct_count = 0 # 统计总正确字符数
# 分类统计各种类型字符的数量和正确数量
uppercase_count = 0
uppercase_correct_count = 0
lowercase_count = 0
lowercase_correct_count = 0
digit_count = 0
digit_correct_count = 0
other_count = 0
other_correct_count = 0
for i in range(total_count):
if original_text[i] == input_text[i]:
correct_count += 1 # 统计总正确字符数
if original_text[i].isupper():
uppercase_correct_count += 1
elif original_text[i].islower():
lowercase_correct_count += 1
elif original_text[i].isdigit():
digit_correct_count += 1
else:
other_correct_count += 1
if original_text[i].isupper():
uppercase_count += 1
elif original_text[i].islower():
lowercase_count += 1
elif original_text[i].isdigit():
digit_count += 1
else:
other_count += 1
# 计算正确率
uppercase_accuracy = uppercase_correct_count / uppercase_count if uppercase_count != 0 else 1
lowercase_accuracy = lowercase_correct_count / lowercase_count if lowercase_count != 0 else 1
digit_accuracy = digit_correct_count / digit_count if digit_count != 0 else 1
other_accuracy = other_correct_count / other_count if other_count != 0 else 1
total_accuracy = correct_count / total_count
# 输出结果
print("大写字母正确率:{:.1%}".format(uppercase_accuracy))
print("小写字母正确率:{:.1%}".format(lowercase_accuracy))
print("数字字符正确率:{:.1%}".format(digit_accuracy))
print("其他字符正确率:{:.1%}".format(other_accuracy))
print("总正确率:{:.1%}".format(total_accuracy))
```
你可以把以上代码保存为一个.py文件,然后在命令行中运行该文件即可得到结果。记得把in.txt文件放在同一目录下。
阅读全文