In: Computer Science
The local driver's license office has asked you to design a program that grades the written portion of the driver's license test. The test has 20 multiple choice questions. Here are the correct answers:
Your program should store these correct answers in an list. (Store each question's correct answer in an element of a String list). The program should ask the user to enter the student's answers for each questions, which should be stored in another list. After the student's answers have been entered, the program should display a message indicating whether the student passed or failed the test. (A student must correctly answer 15 of the 20 questions to pass the test). The program should also display the total number of correctly answered questions, the total number of incorrectly answered questions, and a list showing the question numbers of the incorrectly answered questions.
Notes:
1. pseducode and Python program
2. Your program needs three lists: the first list holds the correct answers, the second list holds the student's answers, and the third list holds the question numbers for those questions that are not answered correctly.
3. Input validation is required for student's answers: when the input is not A, B, C, or D, your program needs to display an error message and get another input. The input validation is done using a while loop.
4. After getting the student's answers, the program will compare the two lists. If a question is answered wrong, the question number of that question will be put into the third list.
Pseudo code:
validAnswer(answer):
loop VALID: until answer is not in 'A','B','C','D':
get answer from student
return answer
driverTest():
loop GET_ANSWER: i from 1 to 20:
get answer from student
answer = validateAns(answer)
store answer in a list: studentAns[i]
loop CHECK_CORRECT: i from 1 to 20:
if studentAns[i] != correctAns[i]:
add question no. 'i+1' to list: incorrectQuesNo
if number of correct answer >= 15:
result = 'Pass'
else:
result = 'Fail'
print the result, number of correct and incorrect answer
and the list incorrectQuesNo
Code: driverTest.py
def validateAns(ans):
'''validation of given answer'''
while(ans not in ('A','B','C','D')):
print("Incorrect answer option(choose option: 'A','B','C','D')")
ans = str(input('Ans: '))
return ans
def driverTest():
'''main function for test'''
totalQuestion = 20
for i in range(totalQuestion):
ans = str(input('Ans for question {}: '.format(i+1)))
ans = validateAns(ans)
studentAns.append(ans)
for i in range(totalQuestion):
if(studentAns[i]!=correctAns[i]):
incorrectQuesNo.append(i+1)
correctNo = (20-len(incorrectQuesNo))
result = correctNo >= 15
print("Result: {}".format('Pass' if result else 'Fail'))
print("Correct: {}, Incorrect: {}".format(correctNo, 20-correctNo))
if incorrectQuesNo:
print("Incorrect answer for Question(s): {}".format(incorrectQuesNo))
if __name__ == "__main__":
correctAns = ['B','D','A','A','C', 'A','B','A','C','D','B','C','D','A','D','C','C','B','D','A']
studentAns = []
incorrectQuesNo = []
driverTest()
code snapshot:
Output: