correct = (["1.B", "6.A", "11.B", "16.C","2.D", "7.B", "12.C", "17.C", "3.A", "8.A", "13.D", "18.B","4.A" ,"9.C", "14.A", "19.D","5.C", "10.D", "15.D", "20.A"])
f = open("MyFile.txt", "r")
user_answers = ([])
count = 0
txt = ""
for x in f.read():
if x == " " or x == "\\":
x = ""
count += 1
if count == 2 :
user_answers.append(txt)
txt = ""
count = 0
txt += x
i = 0
cor = 0
wrong = ([])
while i < len(correct):
if correct[i] == user_answers[i]:
cor += 1
else:
wrong.append(user_answers[i])
i += 1
questions = ([])
for i in wrong:
txt = ""
for w in i:
if w == ".":
questions.append(int(txt))
txt+=w
print("There were {} correct answers".format(cor))
print("There were {} incorrect answers".format(len(questions)))
print("The incorrectly answered questions are {}".format(questions))
f.close()
The file I used for testing looks like:
1. C 6. A 11. B 16. C
2. D 7. B 12. C 17. C
3. A 8. D 13. D 18. D
4. A 9. C 14. A 19. A
5. C 10. D 15. D 20. C
The code only works if there is a new line after 20. C (press enter after that question if a new line isn't already included)
I hope this helps!