当地驾驶执照办公室已要求您编写一个程序,对驾驶执照考试的书面部分进行评分。考试有20个多项选择题。以下是正确的答案:1.A 2.C 3.A 4.A 5.D 6.B 7.C 8.A 9.C 10.B 11.A 12.D 13.C 14.A 15。 D 16.C 17.B 18.B 19.D 20.A
您的程序应将这些正确的答案存储在列表中。该程序应从文本文件中读取学生对20个问题中每个问题的答案,并将答案存储在另一个列表中。在从文件中读取学生的答案后,程序应显示一条消息,指示学生是否通过考试。学生必须正确回答要通过的20个问题中的15个。然后,它应显示正确回答的问题总数,错误回答的问题总数以及显示错误回答问题的问题编号的列表。
如果有人能就我的错误给出一些意见,我们将不胜感激。
def main():
corr_ans_list = ["A", "C", "A", "A", "D", "B",
"C", "A", "C", "B", "A", "D",
"C", "A", "D", "C", "B", "B",
"D", "A"]
user_ans_list = []
corr_count = 0
incorr_count = 0
num_questions = 20
infile = open('user_answers.txt', 'r')
user_ans_list = infile.readlines()
infile.close()
index = 0
print("Q\tocrr\tYour\tStatus")
print("#\tAnswer\tAnswer\n--------------------------")
while index < 20:
print(str(index+1) + "\t" + corr_ans_list[index]+ "\t" + user_ans_list[index],end="\t" )
if user_ans_list[index].strip() == corr_ans_list[index]:
corr_count += 1
index += 1
print(" Correct")
else:
incorr_count += 1
index += 1
print(" Wrong")
percent_corr = (corr_count/num_questions) * 100
percent_corr_fmt = format(percent_corr, ".1f")
print("Grade : ", corr_count , "/", num_questions, " = ",
percent_corr_fmt, sep="")
if percent_corr >= 75:
print("Congratulations!! You passed the exam")
else:
print("Sorry, you did not pass the exam")
main()
我得到的最终答案。
Q Corr Your Status
# Answer Answer
--------------------------
1 A A
Correct
2 C C
Correct
3 A A
Correct
4 A B
Wrong
5 D D
Correct
6 B B
Correct
7 C C
Correct
8 A A
Correct
9 C C
Correct
10 B B
Correct
11 A B
Wrong
12 D D
Correct
13 C C
Correct
14 A A
Correct
15 D D
Correct
16 C B
Wrong
17 B B
Correct
18 B B
Correct
19 D B
Wrong
20 A A
Correct
Grade : 16/20 = 80.0
Congratulations!! You passed the exam
答案 0 :(得分:0)
你不需要这个:
user_ans = infile.readline() # readline reads only one line of a flle
user_ans_list.append(user_ans)
使用readlines
将为您提供文件中所有行的列表
user_ans_list = infile.readlines()
这里你需要strip
。读取线与换行符一致
if user_ans_list[index].strip() == corr_ans_list[index]:
修复ZeroDivisionError:除以零&#34;。当一个数字除以零时,会出现此错误。
num_questions = 10
将上述内容放在上面:
print("Q\tocrr\tYour\tStatus")
你的shouil看起来像这样:
while index < 20:
print(str(index+1) + "\t" + correct_ans_list[index]+ "\t" + user_ans_list[index],end="\t" )
if user_ans_list[index] == corr_ans_list[index]:
corr_count += 1
index += 1
print("Correct")
else:
incorr_count += 1
index += 1
print("Wrong")
答案 1 :(得分:0)
def read_candidate_answers():
outfile=open('answers.txt','r')
answer_list=outfile.read().split()
return answer_list
def correct_answers():
correct_list= ['A','C','A','A','D','B','C','A','C','B','A','D','C','A','D','C','B',\ 'B','D','A']
return correct_list
def result(answer_list,correct_list):
count=0
wrong_answers=[]
for i in range(20):
if answer_list[i]==correct_list[i]:
count+=1
else:
count+=0
wrong_answers.append(i)
if count<15:
print("Failed")
else:
print("Passed")
print("Number of Correct answers: ", count)
print("Number of Incorrect answers: ",20-count)
print("Incorrect answers list is: ", wrong_answers)
answers=read_candidate_answers()
correct=correct_answers()
output=result(answers,correct)
答案 2 :(得分:-1)
问题是user_ans_list
只有一个成员,因为你只从文件中读取一行。
在发布之前尝试对您的错误进行更多研究。谷歌是你的朋友