Else statement executing even the IF statement is TRUE

后端 未结 4 1716
醉酒成梦
醉酒成梦 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:28

    It's a question from a long time ago and I stumbled upon it as I was troubleshooting the very same issue - the solution was actually pretty silly and most probably was also the case - as it's a for loop it iterates through every list element, if even one of those elements doesn't fulfill the if condition, it will automatically trigger the else - pretty self-evident but easy to miss for beginners. Well at least that was the problem in my case :)

    0 讨论(0)
  • 2021-01-23 18:34

    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)
    

    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.

    0 讨论(0)
  • 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'
    
    0 讨论(0)
  • 2021-01-23 18:48

    You have a mixture of tabs and spaces in your code:

    Running cat -A test.py (on Unix) yields

         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$
    ^I^I^I^I^I^I...$
    ^I^I^I^I^I^I...$
                        else:$
                            pogresnoBrojac += 1$
                    slova.remove(slovo)$
    

    The ^I indicate tabs.

    Thus, the else block is not being interpreted as being at the indentation level on which it appears to be.

    Your python code should never mix tabs and spaces for indentation. You can check that your script is not mixing tabs and spaces by running python -t script.py.

    In Python you must commit to using either only spaces or only tabs for indentation. PEP8 recommends spaces-only indentation.

    You can convert tabs to spaces using the reindent.py program.

    0 讨论(0)
提交回复
热议问题