2

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)

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

How to fix this issue?

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

lbartolic
  • 1,165
  • 2
  • 12
  • 24

4 Answers4

4

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.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • There's no doubt that this is very good advice. However, aren't in this example the `if` and the `else` indented using spaces alone? I am sure I am missing something obvious here. – NPE Mar 24 '13 at 10:52
  • thank you for this one, but i can't fix this problem. when I indent it on the right way, I keep getting this issue. – lbartolic Mar 24 '13 at 10:55
  • @NPE: You are right; this may not be the real problem. However, in my experience when the basic rules are conformed to, mysterious errors tend to disappear. – unutbu Mar 24 '13 at 11:04
  • 1
    @lucro93: In that case, you really need to show us runnable code that manifests the problem. – unutbu Mar 24 '13 at 11:06
  • i found a sollution! i'll answer – lbartolic Mar 24 '13 at 11:18
3

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'
Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • thanks for the answer, but my else statement is lined with the if statement, if you wanted to say that else statement is lined with the for one? – lbartolic Mar 24 '13 at 10:48
  • else is executed in another for-loop. See my main comment at question. – ZF007 Aug 11 '19 at 23:21
0

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 :)

-1

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.

ZF007
  • 3,708
  • 8
  • 29
  • 48
lbartolic
  • 1,165
  • 2
  • 12
  • 24