jeudi 30 avril 2015

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.

The problem is, altough they ARE the same (looking at a print() output), the script does not see them as though:

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":

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)!

Aucun commentaire:

Enregistrer un commentaire