Else statement executing even the IF statement is TRUE

后端 未结 4 1807
醉酒成梦
醉酒成梦 2021-01-23 17:55

I have a problem in Python language that is described in a title.

 for slovo in slova:
        if pygame.mouse.get_pressed()[0] and slovo[\"rect         


        
4条回答
  •  没有蜡笔的小新
    2021-01-23 18:39

    So, even this IF statement is true, ELSE statement is being executed!

    I can assure you that this is not what happens.

    I notice that in the outline of your code the if is inside a for loop. Make sure that in your actual code the else is not accidentally lined up with the for instead of the if. I've seen this mistake more than once.

    In Python, for-else is a valid construct. For example, the following is perfectly valid Python:

    for i in range(10):
        if i < 100:
            pass
    else:
        print 'In else clause'
    

    When run, this prints out In else clause.

    Contrast this with the following, which doesn't print anything when run:

    for i in range(10):
        if i < 100:
            pass
        else:
            print 'In else clause'
    

提交回复
热议问题