简体   繁体   English

如果==不适用于python中的列表。 不知道我在做什么错。 数据的print()表明它们是相等的……我缺少什么?

[英]IF == not working for lists in python. No idea what I am doing wrong. A print() of the data reveals they are equal…what am I missing?

So, here is my code. 所以,这是我的代码。 It is meant to take answers from a user. 它旨在从用户那里得到答案。 Store their answers in a list (answerlist. Compare their answers to a list made from the lines of a answers.txt file. 将他们的答案存储在一个列表中(answerlist。将他们的答案与由Answers.txt文件的各行组成的列表进行比较。

The problem is, altough they ARE the same (looking at a print() output), the script does not see them as though: 问题是,尽管它们是相同的(看一看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)

Contents of "answers.txt": “ answers.txt”的内容:

A
B
C
D
E
F
G
H
I
J
K

And my output when he script is run (after answering with the correct answers) is: 当他运行脚本时(在回答正确答案后),我的输出是:

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

Thanks for any help! 谢谢你的帮助! I am sure it will be something simple as I am pretty noobish (as you may tell from my bad code :P)! 我肯定这会很简单,因为我有点笨拙(正如您可能会从我的错误代码:P中看到的那样)!

The answers read from the file have trailing newlines that break the comparison. 从文件中读取的答案带有结尾的换行符,从而破坏了比较。 Replace real_answerlist.append(line) with real_answerlist.append(line.strip()) . real_answerlist.append(line)替换为real_answerlist.append(line.strip())

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:在稍后的打印语句中出现子脚本列表和访问数据的问题,我做错了什么? - Python: Trouble sub scripting lists and accessing data in later print statement, what am I doing wrong? 我究竟做错了什么。 我想在调用 function 时打印到 fv 值 - What am I doing wrong. I want to print to the fv value when I invoke the function 在 Python 中的列表反向遍历中我做错了什么? - What am I doing wrong in this reverse traversion of lists in Python? 将带字母的电话号码转换为python中的所有数字。 我究竟做错了什么? - translating phone number with letter to all numbers in python. what am i doing wrong? 尝试使用Python验证SHA1消息签名。我究竟做错了什么? - Trying to verify SHA1 message signature using Python. What am I doing wrong? 无法使用 python 中的 mechanize 登录。 我究竟做错了什么? - Can't login using mechanize from python. What am I doing wrong? Lexer在Python中输出“ TypeError:write()参数必须为str,而不是字节”。 我究竟做错了什么? - Lexer Outputs “TypeError: write() argument must be str, not bytes” in Python. What am I doing wrong? 不知道我做错了什么...... - ||| PYTHON ||| - Not sure what I am doing wrong… - ||| PYTHON ||| Python查找功能不起作用。 我究竟做错了什么? - Python find function not working. What am I doing wrong? Python:SOAP API 不起作用,我做错了什么? - Python: SOAP API not working, what am I doing wrong?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM