ThatsNotMyName
ThatsNotMyName

Reputation: 582

Python - if statement inside while loop not responding

I'm having trouble with an if statement inside a while loop.

while pressed == 8 :
    print(answerlistx[randomimage], answerlisty[randomimage])
    entryx = e1.get()
    entryy = e2.get()
    answerx = answerlistx[randomimage]
    answery = answerlisty[randomimage]
    print(entryx, entryy)
    if e1 == answerx and e2 == answery:
        print("correct")
        canvas.delete(images)
        randomimage = random.randrange(0,49+1)
        scorecounter = scorecounter + 1
        game = PhotoImage(file=imagelist[randomimage])
        images = canvas.create_image(30, 65, image = game, anchor = NW)
        e1.delete(0, END)   
        e2.delete(0, END)
        pressed = ''
    else:
        print("incorrect")
        e1.delete(0, END)   
        e2.delete(0, END)
        pressed = ''

The while loop is supposed to check to see if the inputs from the entry widget match the the answer but even when the answer is correct it goes to the else statement. I have 2 print statements just before the if statement which prints the input and what the answer is just incase it didn't have that but it does display it both correctly. I also thought it might have been a mix up with strings and integers so I changed all the answers in the answerlist to strings with no luck. Anyone able to figure out what is wrong with it? Thanks in advance.

Upvotes: 2

Views: 5246

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1124728

You are testing if the entry objects are the same as the answers. Use the actual values:

if entryx == answerx and entryy == answery:

instead of testing against e1 and e2.

Upvotes: 6

Related Questions