Skip to main content
 首页 » 编程设计

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

2025年05月04日63qlqwjy

所以,这是我的代码。 它旨在从用户那里获取答案。 将他们的答案存储在列表中(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())。