Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
IF==不适用于python中的列表。不知道我做错了什么。数据的打印()显示它们是相等的…我遗漏了什么?_Python_List_If Statement_Compare - Fatal编程技术网

IF==不适用于python中的列表。不知道我做错了什么。数据的打印()显示它们是相等的…我遗漏了什么?

IF==不适用于python中的列表。不知道我做错了什么。数据的打印()显示它们是相等的…我遗漏了什么?,python,list,if-statement,compare,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 F

这是我的代码。 它意味着从用户那里获取答案。 将他们的答案存储在一个列表(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”的内容:

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


谢谢你的帮助!我相信这会很简单,因为我是一个非常粗鲁的人(你可以从我糟糕的代码中看出:p)!

从文件中读取的答案有尾随的新行,打破了比较。将
real\u answerlist.append(line)
替换为
real\u answerlist.append(line.strip())

从文件中读取的答案后面有中断比较的换行符。将
real\u answerlist.append(line)
替换为
real\u answerlist.append(line.strip())

是谁教你读那样的文件的?@Gullydwarf-有没有更好的方法逐行阅读并将它们放入列表中?我对with语句的使用更感兴趣。我通常创建一个文件对象并使用“readLines()“@Gullydwarf:with使用
的好处是,你不必担心在完成或出现任何异常时关闭文件,因为这将自动完成。谁教你读那样的文件?@Gullydwarf-有没有更好的方法逐行阅读并将其放入列表中?我对使用更感兴趣我通常创建一个file对象并使用'readLines()'@Gullydwarf:with
使用
的好处是,您不必关心在完成或出现任何异常时关闭文件,因为这将自动完成。
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