In: Computer Science
The driver’s license office DMV has asked you to write a program that grades the written portion of the driver’s license questions. The questions has 20 multiple-choice questions. Here are the correct answers:
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
B |
D |
A |
A |
A |
C |
B |
C |
D |
A |
D |
C |
C |
D |
D |
B |
A |
B |
C |
D |
Your program should store these correct answers in a list. The program should first ask the user for the filename that stores the answers. Then, read the student’s answers for each of the 20 questions from a text file specified by the user and store the answers in another list. (I have provided a sample file called answers1.txt) After the student’s answers have been read from the file, the program should display a message indicating whether the student passed ('You passed") or failed ("You failed") the exam. (A student must correctly answer 15 of the 20 questions to pass the exam.) It should then 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 (and the correct answers). For example, the given asnwers1.txt file will generate the following output:
enter file name: answers1.txt
You failed
Total correct: 13 Questions # = 3, 4, 6, 7, 8, 10, 12, 13, 15, 16,
17, 19, 20
Total incorrect: 7 Questions # = 1, 2, 5, 9, 11, 14, 18
Correct answers for incorrect questions are:
1 : B
2 : D
5 : A
9 : D
11 : D
14 : D
18 : B
Here is the completed code for this problem. Assuming the language is Python. Please don’t forget to mention the language while you post a question in future. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.
#code
# hardcoding a list of correct answers correct_answers = ['B', 'D', 'A', 'A', 'A', 'C', 'B', 'C', 'D', 'A', 'D', 'C', 'C', 'D', 'D', 'B', 'A', 'B', 'C', 'D'] # asking, reading file name filename = input("enter file name: ") # opening file in read mode, assuming file exist file = open(filename, 'r') # assuming the answers are listed in a single line on the file # reading a line, splitting by white space to get a list of answers answers = file.readline().strip().split() # closing file file.close() # creating two lists to store question numbers of right and wrong answers correct = [] wrong = [] # looping through answers list, assuming len(answers) is 20 for i in range(len(answers)): # comparing answers at index i if answers[i] == correct_answers[i]: # correct, adding i+1 (question numbers start from 1) to correct answers list correct.append(i + 1) else: # wrong, appending i+1 to wrong answers list wrong.append(i + 1) # if user managed to answer at least 15 right answers, he/she passed if len(correct) >= 15: print("You passed") else: print("You failed") # printing count of right/wrong answers and the corresponding question numbers # ', '.join([str(i) for i in correct]) will print each element in correct comma separated print('Total correct: {} Questions # = {}'.format(len(correct), ', '.join([str(i) for i in correct]))) print('Total incorrect: {} Questions # = {}'.format(len(wrong), ', '.join([str(i) for i in wrong]))) # if user had submitted any wrong answer(s), displaying each question number and the right answer if len(wrong) > 0: print('Correct answers for incorrect questions are:') for i in wrong: print('{} : {}'.format(i, correct_answers[i - 1]))
#output
enter file name: answers.txt
You passed
Total correct: 16 Questions # = 2, 3, 4, 5, 6, 7, 8, 10, 11, 12,
13, 14, 15, 17, 19, 20
Total incorrect: 4 Questions # = 1, 9, 16, 18
Correct answers for incorrect questions are:
1 : B
9 : D
16 : B
18 : B
#answers.txt file that I used
C D A A A C B C A A D C C D D D A A C D