2

我在 while 循环中遇到了 if 语句的问题。

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 = ''

while 循环应该检查条目小部件的输入是否与答案匹配,但即使答案正确,它也会转到 else 语句。我在打印输入的 if 语句之前有 2 个打印语句,而答案是什么,以防万一它没有,但它确实正确显示了它。我还认为这可能是字符串和整数的混合,所以我将答案列表中的所有答案都更改为字符串,但没有运气。任何人都能够弄清楚它有什么问题吗?提前致谢。

4

1 回答 1

6

您正在测试条目对象是否与答案相同。使用实际值:

if entryx == answerx and entryy == answery:

而不是针对e1and进行测试e2

于 2013-07-19T09:24:06.877 回答