【发布时间】:2017-04-24 09:36:08
【问题描述】:
我对 python 还很陌生,本学期才开始学习这门课程。我正在努力寻找一种编写代码的方法,该代码将正确答案存储为列表,然后从 txt 文件中读取 20 个问题中每个问题的学生答案,并将答案存储在另一个列表中。之后,我想比较列表,然后打印他们的答案,程序将显示一条消息,指示学生是否通过,(15 或更大的正确是通过)和总数正确和总数不正确。所以例如正确的答案为 A,C,A,A,D,B,C,A,C,B,A,D,C,A,D,C,B,B,D,A。对于学生的答案将只是创建您的自己的文本文件进行测试。任何帮助将不胜感激我目前的格式似乎不起作用,如下所示。
def main():
total = 0
index = 0
answers = [ 'A', 'C', 'A', 'A', 'D',\
'B', 'C', 'A', 'C', 'B',\
'A', 'D', 'C', 'A', 'D',\
'C', 'B', 'B', 'D', 'A']
student_answers = open('student_solution.txt', 'r')
for answer in student_answers:
print(answer.strip())
while index in answers == student_answers:
if student_answers[0] == answers[0]:
total +=1
else:
total +=0
student_answers.close()
print('Total correct answers: ', total)
print('Total of incorrect answers: ', 20 - total)
if total >= 15:
print('Congratulations! You passed the exam.')
else:
print('Sorry, you have failed the exam.')
main()
这是似乎仍然存在问题的更新程序。我正在使用的学生答案是 A C A A D B C A C B A D C A D C B B D A C A A D B C A C B A D C A D C B B D D
def main():
total = 0
index = 0
answers = [ 'A', 'C', 'A', 'A', 'D',\
'B', 'C', 'A', 'C', 'B',\
'A', 'D', 'C', 'A', 'D',\
'C', 'B', 'B', 'D', 'A']
infile = open('student_solution.txt', 'r')
student_answers = infile.readline()
infile.close()
print(student_answers)
for answer in student_answers:
for y in range(len(answer)):
if answer[y] == answers[y]:
total += 1
print('Total correct answers: ', total)
print('Total of incorrect answers: ', 20 - total)
if total >= 15:
print('Congratulations! You passed the exam.')
else:
print('Sorry, you have failed the exam.')
main()
【问题讨论】:
-
while index in answers == student_answers:这不是你认为的那样 -
有问题吗?