执行甚至IF语句的其他语句为TRUE

编程入门 行业动态 更新时间:2024-10-11 13:26:09
本文介绍了执行甚至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

本文发布于:2023-10-19 17:52:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1508311.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语句   TRUE

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!