某心理测试题全部由单选题组成,每个题目有3个选项,不同选项得分不同(但每题分数都是在10分-99分之间),答案和分数保存在一个文件answer.txt中,格式为: 1A10B20C30 2A20B10C30 . 20A30B10C20 其中前面的数字为题号。从键盘输入某位考生答案,例如: ABCC.. 编程计算并输出该考生的成绩。
时间: 2023-06-18 14:07:02 浏览: 181
假设题目数量为n,可以按照如下步骤计算考生的成绩:
1. 从answer.txt文件中读取每个题目的正确答案和分数,保存为一个字典,例如:
```
{'1A': 10, '1B': 20, '1C': 30, '2A': 20, '2B': 10, '2C': 30, ..., 'nA': x, 'nB': y, 'nC': z}
```
2. 从键盘输入考生的答案,保存为一个字符串,例如:"ABCC..."
3. 遍历考生的答案字符串,对于每个题目,根据答案和正确答案从字典中获取分数,累加到总分中。
4. 根据总分计算得分率,例如:
```
score_rate = total_score / (n * 99)
```
5. 根据得分率计算百分制成绩,例如:
```
score = score_rate * 100
```
下面是完整代码:
```python
def read_answers(filename):
answers = {}
with open(filename, 'r') as f:
for line in f:
parts = line.strip().split()
for part in parts:
q, ans, score = part[0], part[1], int(part[2:])
answers[q+ans] = score
return answers
def calculate_score(answers, response):
total_score = 0
for i, ans in enumerate(response):
q = str(i+1)
if q+'A' in answers and ans == 'A':
total_score += answers[q+'A']
elif q+'B' in answers and ans == 'B':
total_score += answers[q+'B']
elif q+'C' in answers and ans == 'C':
total_score += answers[q+'C']
score_rate = total_score / (len(response) * 99)
score = score_rate * 100
return score
answers = read_answers('answer.txt')
response = input('请输入考生答案:')
score = calculate_score(answers, response)
print('考生得分:{}'.format(score))
```