怎么将standard = f1.readlines()逐行读取答案改成逐个,用;间隔
84浏览 · 2024-02-29 22:51:35
如果你想将`standard = f1.readlines()`逐行读取[答案](https://geek.csdn.net/educolumn/1a150c385cca90862798ac333f51cb29?spm=1055.2569.3001.10083)改为逐个,并且用分号`;`来分隔[答案](https://geek.csdn.net/educolumn/1a150c385cca90862798ac333f51cb29?spm=1055.2569.3001.10083),可以使用`split()`方法将每行[答案](https://geek.csdn.net/educolumn/1a150c385cca90862798ac333f51cb29?spm=1055.2569.3001.10083)拆分为一个个单独的答案,并使用`extend()`方法将它们添加到[列表](https://geek.csdn.net/educolumn/ba94496e6cfa8630df5d047358ad9719?dp_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NDQ0MDg2MiwiZXhwIjoxNzA3MzcxOTM4LCJpYXQiOjE3MDY3NjcxMzgsInVzZXJuYW1lIjoid2VpeGluXzY4NjQ1NjQ1In0.RrTYEnMNYPC7AQdoij4SBb0kKEgHoyvF-bZOG2eGQvc&spm=1055.2569.3001.10083)中。以下是修改后的[代码](https://geek.csdn.net/educolumn/1572ef9b473b4e00f6b2bf6d428b7c27?spm=1055.2569.3001.10083)示例:
```python
# 打开标准答案文件和答题答案文件
with open('standard_answers.txt', 'r') as f1, open('student_answers.txt', 'r') as f2:
# 逐个读取答案,并用分号分隔
standard = []
for answer in f1:
standard.extend(answer.strip().split(';'))
student = []
for answer in f2:
student.extend(answer.strip().split(';'))
# 计算每个答案是否正确,并计算分数
score = 0
wrong = []
for i in range(len(standard)):
if standard[i] == student[i]:
score += 1
else:
wrong.append(f"第{i+1}题:标准答案是{standard[i]},你的答案是{student[i]}")
# 输出分数和错误的内容
print(f"你的得分是{score}分(满分{len(standard)