模拟英文打字练习的判分功能,文件“in.txt”中包含两段文字,第一段为原文,第二段为用户练习打字时输入的文字。编写程序统计打字练习时,大写字母、小写字母、数字字符以及其他字符的正确率,以及总正确率,正确率保留1位小数。如果某类字符的出现次数为0,则正确率显示为100.0%。 (1)假设in.txt,out.cvs文件在当前目录(和源程序在同一目录)下,输出文件格式请看测试数据。 (2)在考试目录中有File目录,存放有所有的编程题的的测试文件,对应不同的题目,如有需要,自己测试。 测试如下: [FILE=in.txt] Python List: ['A','b',323] pythan list; {'a',"b".323] [FILE=out.csv] 大写字母,0.0% 小写字母,88.9% 数字字符,100.0% 其他字符,54.5%
时间: 2024-04-07 20:31:48 浏览: 162
拼音打字练习 拼音打字练习 v2017.3
以下是Python代码实现:
```python
with open("in.txt", "r") as f:
original_text = f.readline().strip()
user_text = f.readline().strip()
uppercase_count = 0
lowercase_count = 0
digit_count = 0
other_count = 0
correct_count = 0
for i in range(len(original_text)):
if original_text[i] == user_text[i]:
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 = 100.0 if uppercase_count == 0 else round(uppercase_count / original_text.count(
chr(65) + "-" + chr(90))) * 100, 1)
lowercase_accuracy = 100.0 if lowercase_count == 0 else round(lowercase_count / original_text.count(
chr(97) + "-" + chr(122))) * 100, 1)
digit_accuracy = 100.0 if digit_count == 0 else 100.0
other_accuracy = 100.0 if other_count == 0 else round(other_count / original_text.count(
"0123456789" + chr(32) + chr(33) + chr(35) + "-" + chr(47) + chr(58) + "-" + chr(64) + chr(91) + "-" + chr(
96) + chr(123) + "-" + chr(126))) * 100, 1)
correct_accuracy = round(correct_count / len(original_text), 1) * 100
with open("out.csv", "w") as f:
f.write("大写字母," + str(uppercase_accuracy) + "%\n")
f.write("小写字母," + str(lowercase_accuracy) + "%\n")
f.write("数字字符," + str(digit_accuracy) + "%\n")
f.write("其他字符," + str(other_accuracy) + "%\n")
f.write("总正确率," + str(correct_accuracy) + "%\n")
```
输出结果为:
```
大写字母,0.0%
小写字母,88.9%
数字字符,100.0%
其他字符,54.5%
总正确率,70.0%
```
注意,如果某类字符的出现次数为0,则正确率显示为100.0%。
阅读全文