Python Forum
variable isn't recognized by if structure
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
variable isn't recognized by if structure
#1
Hey there!

I don't get this:

I have a code that goes like this (simplified, but essentially that):

def q():
    loop = True
    CC = 0
    WC = 3
    while loop == True:
        for event in pygame.event.get():
            if event.type == pygame.MOUSEBUTTONUP:
                mposx,mposy = pygame.mouse.get_pos()
                
                if 200 < mposx < 300 and 150 < mposy < 200:
                    print ('Correct')
                    CC = CC+1
                if 400 < mposx < 500 and 150 < mposy < 200:
                    print ('Wrong')
                    WC = WC-1

        print (CC)
        print (WC)
        if CC == 10:
            loop = False
            check = True
            return check
            print ('Win')
        if WC == 0:
            loop = False
            check = False
            return check
            print ('Fail')


    pygame.display.update()
    clock.tick(30)
But when I run that and I click on the wrong button in the game, it prints WC as running down to 0, but then restores it to 3 and drops out of the loop, the second time it hits 0, but without printing 'Fail'.

So it basically says in the print line "WC is O" and the next line "if WC == 0" doesnt get activated.

I am so confused and would be very thankful for any help!

----------------------------------------------------------------

Update: When I just run this:

q()
print ('End')
it works, but when I run this:

q()
check = q()
print (check)
print ('End')
the problem I described above occurs
Maybe that's helping? For me it's just confusing...
Reply


Messages In This Thread
variable isn't recognized by if structure - by Prof_Jar_Jar - Dec-23-2018, 04:29 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020