Else statement executing even the IF statement is TRUE

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

提交回复
热议问题