比较列表 - Python 作业
correct_ans = ['B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B', 'C', \
'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A']
这是我从 txt 文件导入列表的语句。
# 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)
此逻辑继续返回所有答案都是错误的。从视觉上比较我的“ Correct_ans”列表和“driver_ans”列表,这是不正确的。我做错了什么?!
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?!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只是猜测。如果 testcores.txt 记住了内容
,则 driver_ans 可能会被
尝试
Only guessing. If testscores.txt has the content
keep in mind, that driver_ans will be
try maybe
readlines() 函数返回包含尾随换行符的行。所以,尝试:
The
readlines()
function returns lines that include the trailing newline. So, try: