使用python的驾照考试问题

使用python的驾照考试问题,python,Python,当地驾照办公室要求您编写一个程序,对驾照考试的笔试部分进行评分。这次考试有20道选择题。以下是正确答案:1.a2.c3.a4.a5.d6.b7.c8.a9.c10.b11.a12.d13.c14.a15.d16.c17.b18.b19.d20.A 您的程序应将这些正确答案存储在列表中。程序应该从文本文件中读取学生对20个问题的答案,并将答案存储在另一个列表中。从文件中读取学生的答案后,程序应显示一条消息,指示学生是否通过考试。学生必须正确回答20个问题中的15个才能通过考试。然后,它应该显示正确

当地驾照办公室要求您编写一个程序,对驾照考试的笔试部分进行评分。这次考试有20道选择题。以下是正确答案:1.a2.c3.a4.a5.d6.b7.c8.a9.c10.b11.a12.d13.c14.a15.d16.c17.b18.b19.d20.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()
你不需要这个:

user_ans = infile.readline()   # readline reads only one line of a flle
user_ans_list.append(user_ans)
使用
readlines
将为您提供文件中所有行的列表

user_ans_list = infile.readlines()
这里需要
条带
。读线采用换行符换行

if user_ans_list[index].strip() == corr_ans_list[index]:
若要修复ZeroDivision错误,请执行以下操作:被零除“。当数字被零除时,会出现此错误

num_questions = 10
把这个放在上面,同时:

print("Q\tocrr\tYour\tStatus")
您的while应该如下所示:

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")
索引<20时:
打印(str(索引+1)+“\t”+正确的索引列表[索引]+“\t”+用户索引列表[索引],end=“\t”)
如果用户列表[索引]==corr\u ans\u列表[索引]:
更正计数+=1
指数+=1
打印(“正确”)
其他:
进线计数+=1
指数+=1
打印(“错误”)

问题在于
用户列表
只有一个成员,因为您只读取文件中的一行。

def read\u candidate\u answers():
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)
outfile=open('answers.txt','r') answer_list=outfile.read().split() 返回答案列表 def correct_answers(): 正确的列表=['A'、'C'、'A'、'D'、'B'、'C'、'A'、'C'、'B'、'A'、'D'、'C'、'A'、'D'、'C'、'B'、'B'、'D'、'A'] 返回正确的列表 def结果(答案列表、正确列表): 计数=0 错误的答案=[] 对于范围(20)内的i: 如果答案列表[i]==正确的列表[i]: 计数+=1 其他: 计数+=0 错误答案。附加(i)
如果你需要了解python才能拿到驾照?你住在哪里?@Pradhan这就是问题的答案你会遇到什么错误?索引器:列出索引超出范围第30行出现新错误“ZeroDivisionError:division by zero”我现在唯一的问题是在第30行,错误是“ZeroDivisionError:division by zero”“你给了num_questions=0??num_questions=20,我想现在一切都解决了。最后一个问题,我如何显示一个显示错误答案的列表我在运行模块后得到了这个问题。Q corr你的状态#答案------------------成绩:16/20=80.0恭喜!!您通过了examYou需要将正在读取的文件转换为列表(使用.split()方法)以将其与正确答案列表进行比较。此外,您不需要找到百分比-问题不是问百分比,而是问计数。请参考我上方发布的解决方案。
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)