我在标题中描述的 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
发布评论