2

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.

5
  • Just use common sense. If it goes to else, e1 == answerx and e2 == answery is not true. PERIOD. now, figure out why. Have you checked what the type and value of those variables? some trace messages can do miracles... Commented Jul 19, 2013 at 9:24
  • @KarolyHorvath The OP says "even when the answer is correct it goes to the else statement". Commented Jul 19, 2013 at 9:25
  • 1
    Just an observation from looking at the code but are you sure you are not meant to be comparing entryx and entryy with answerx and answery? as this is what you are printing before the comparison. Currently you are comparing e1 and e2 which you are not printing for visual validation. Commented Jul 19, 2013 at 9:25
  • 1
    @Haidro: I trust the python compiler/runtime. I don't trust the OP. It's as simple as that. Commented Jul 19, 2013 at 9:26
  • @KarolyHorvath That's not very nice tbh Commented Jul 19, 2013 at 9:28

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

3 Comments

As it stands this is just speculation although I suspect this is the correct answer.
Yep, it's correct, but it's telling me I can't accept the answer yet and thanks heaps for the answer Martijn Pieters!
Glad to have been of help! @justinfay: it was a little more than just speculation. :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.