mardi 7 novembre 2017

How to call back to files in an if statement in python

I'm trying to make a code that takes a line from the file and prints it and then you type in an answer and if it matches another line it says 'correct'. that part is working but when we get to the 4th question it just keeps repeating and then gives an error. In the text file the lines are separated by a /.

 def Q():
        switch=False
        a = 0
        b = 1
        while switch == False:
            file = open("AA.txt", "r")
            for line in file:
                sec=line.split("/")
            print(sec[a])
            answer = input ("Type your answer: ")
            if answer == (sec[b]):
                print ("Correct!")
                if b == 7:
                    print ('Done')
                    switch==True
                elif b < 7:
                    a = a + 2
                    b = b + 2
                    switch==True
            else:
                print ("Wrong it's ", sec[b])
                a = a + 2
                b = b + 2

        file.close()
    Q ()

This is the text file:

Slash separates stuff./When was the Battle of Hastings? 1066, 1078 or 1088/1066/When was the Great Fire of London? 1777 or 1666/1666/How many wives does Henry the VIII have? 8 or 6/6/When was the Wall Street Crash? 1929 or 1933/1929/

Aucun commentaire:

Enregistrer un commentaire