python - IF == 不适用于 python 中的列表。不知道我做错了什么。数据的 print() 显示它们是相等的......我错过了什么?

标签 python list if-statement compare

所以,这是我的代码。 它旨在从用户那里获取答案。 将他们的答案存储在列表中(answerlist. 将他们的答案与由 answers.txt 文件的行组成的列表进行比较。

问题是,尽管它们是相同的(查看 print() 输出),但脚本看不到它们:

import time
import os

answerlist = []
check = "N"
score=0
real_answerlist=[]
checkanswerlist=[]



name = input("\nWhat is your FIRST name?\n")
surname = input("\nWhat is your SECOND/surname?\n")
classname = input("\nWhat is your class (e.g. \"8A\")\n")
while check != "Y":
    print("Are these detail correct? : "+name + " "+surname+" " + classname+"\n")
    check = input("Enter (Y or N)\n")


#ASK QUESTIONS

for i in range(1, 11):
    answerlist.insert(i,input("The answer to question "+str(i)+" is: \n"))


#show answers
for i in range (1, 5):
    print("Your answers will be shown below in "+str(5-i)+"seconds... ")
    time.sleep(1)
    os.system('cls')

for i in range(0,len(answerlist)):
    print(str(i+1)+": "+answerlist[i])

#Ask if want any answers changing?

while check != "N":
    check=input("\n Do you want to change any answers? (Y or N)\n")
    if check!="N":
        question_to_correct=input("Which question do you want to change?\n")
        corrected_answer=input("What do you want to change your answer to?\n")
        answerlist[int(question_to_correct)-1]=corrected_answer
        print("Here are your answers...")
        for i in range(0,len(answerlist)):
            print(str(i+1)+": "+answerlist[i])

#Place result in to text file:
string_to_write=",".join(answerlist)

f = open("Y8Answers.csv","a") #opens file with name of "username"
f.write("\n"+name+","+surname+","+classname+","+string_to_write)
f.close()


print("Test completed.")

#show results
for i in range (1, 3):
    print("Your answers will be shown below in "+str(5-i)+"seconds... ")
    time.sleep(1)
    os.system('cls')

p=0;
with open("answers.txt") as f:          #Shove all the real answers from answers.txt in to a list "real_answerlist".
        for line in f:
            real_answerlist.append(line)

        for i in range (0,len(answerlist)):             #if their answer == answer in list, add one to score.
            if str(answerlist[i])==str(real_answerlist[i]):
                score=score+1
                print(answerlist[i]+" "+real_answerlist[i])
            else:
                print("Question "+str(i+1)+" is incorrect."+" Your answer: "+str(answerlist[i])+" real is: "+str(real_answerlist[i]))

print (score)

“answers.txt”的内容:

A
B
C
D
E
F
G
H
I
J
K

当他运行脚本时(回答正确答案后)我的输出是:

Question 1 is incorrect. Your answer: A real is: A

Question 2 is incorrect. Your answer: B real is: B

Question 3 is incorrect. Your answer: C real is: C

Question 4 is incorrect. Your answer: D real is: D

Question 5 is incorrect. Your answer: E real is: E

Question 6 is incorrect. Your answer: F real is: F

Question 7 is incorrect. Your answer: G real is: G

Question 8 is incorrect. Your answer: H real is: H

Question 9 is incorrect. Your answer: I real is: I

Question 10 is incorrect. Your answer: J real is: J

0

Process finished with exit code 0

感谢您的帮助!我相信这会很简单,因为我很菜鸟(你可能会从我的错误代码中看出 :P)!

最佳答案

从文件中读取的答案具有破坏比较的尾随换行符。将 real_answerlist.append(line) 替换为 real_answerlist.append(line.strip())

关于python - IF == 不适用于 python 中的列表。不知道我做错了什么。数据的 print() 显示它们是相等的......我错过了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29962187/

相关文章:

python - 如何对 x-y 坐标列表进行排序

python - 如何循环遍历2个数组?

python - str(列表[索引]) : IndexError: list index out of range

ios - 仅显示部分 coreData 数组

python - 连接到mysql

python - 提高 DNN 模型的准确性

python - google-app-engine 全文搜索,哪个更好, "google custom search"或 whoosh

c++ - C++中如何通过引用传递结构体?

php - 如何在 php 中区分一个值是零还是 NULL?

c++ - 为什么两个相同数组的元素彼此相等