correct_ans = ['B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B', 'C', \
'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A']
here is my statement to import the list from txt file
# import user answers into a list
infile = open('testscores.txt', 'r')
driver_ans = infile.readlines()
infile.close()
driver_ans = ['B', 'D', 'A', 'A', 'C', 'B', 'B', 'A', 'C', 'D', 'B', 'C', \
'D', 'A', 'D', 'C', 'C', 'B', 开发者_如何学运维'D', 'A']
for index in range(0, 20):
if driver_ans[index] == correct_ans[index]:
total_correct += 1
else:
wrong_ans.append(index + 1)
This logic continues to return that all are wrong answers. This is not correct comparing visually my "correct_ans" list and my "driver_ans" list. What am I doing wrong?!
Only guessing. If testscores.txt has the content
B
D
A
A
...
keep in mind, that driver_ans will be
['B\n', 'D\n', 'A\n', 'A\n', ...
try maybe
driver_ans = [x.strip ('\n') for x in infile.readlines()]
The readlines()
function returns lines that include the trailing newline. So, try:
driver_ans = [x.strip() for x in infile.readlines()]
精彩评论