执行甚至IF语句的其他语句为TRUE [英] Else statement executing even the IF statement is TRUE

查看:130
本文介绍了执行甚至IF语句的其他语句为TRUE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在标题中描述的 Python 语言有问题。

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"].collidepoint(pygame.mouse.get_pos()):
            for i in range (len(randRijec)):
                if slovo["name"] in randRijec[i]:
                    if i == 0:
                        slovo1 = randRijec[i].upper()
                        prvoSlovo = 1
                    ...
                    ...
                else:
                    pogresnoBrojac += 1
            slova.remove(slovo)

因此,即使这个IF语句是 true ,也正在执行ELSE语句!但是,如果 if语句已满足,则应跳过 else语句

So, even this IF statement is true, ELSE statement is being executed! However, else statement should be skipped if the if statement is fulfilled.

如何解决此问题?

ps我之前几次遇到过这个问题而且我无法解决它...

p.s. I've had this problem few times before and I was not able to solve it...

推荐答案

下一个解决方案解决了我的问题:

Next solution fixed my problem:

    for slovo in slova:
        if pygame.mouse.get_pressed()[0] and slovo["rect"].collidepoint(pygame.mouse.get_pos()):
            xy = 0
            for i in range (len(randRijec)):
                if slovo["name"] in randRijec[i]:
                    xy = 1
                    if i == 0:
                        slovo1 = randRijec[i].upper()
                        prvoSlovo = 1
                        break
                    if i == 1:
                        slovo2 = randRijec[i].upper()
                        drugoSlovo = 1
                        break
            slova.remove(slovo)
            if xy == 0:
                pogresnoBrojac += 1

    ...
    ...
    ...

    xy = 1
    pygame.display.update()
    time.tick(value)

所以,我有刚刚添加了 xy 计数器,使我的代码能够正常工作。当 IF 语句满足时, xy 变为 1 ,如果 IF 语句未满足, xy 设置为 0 ,然后执行此 else 语句。在代码的末尾, xy 再次设置为 1 ,以防止执行此else( if xy == 0 )块。

So, I have just added that xy counter that makes my code work how it should work. When IF statement is met, xy becomes 1, if IF statement isn't fulfilled, xy is set to 0 and then this "else" statement executes. At the end of the code, xy is set to 1 again to prevent executing this "else" (if xy == 0) block.

感谢您的帮助!

这篇关于执行甚至IF语句的其他语句为TRUE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆