Variable does not change in List (Resolved)

I’m trying to code a up a game where you guess a color’s rgb values, using the arrow keys (ignore how horrible of a concept that is for now), but the list I’m using can’t seem to change the variable it’s supposed to be’s value.

(Note, the screen draws everything fine. It’s specifically the fact that certain functions aren’t changing values they’re supposed to.)

#^^code above this, importing pygame and random.
running = True
#randomly generate colors for this.
red = 0
green = 0
blue = 0

userred = 0
usergreen = 0
userblue = 0

userlist = [userred, usergreen, userblue]
points = 0 #this number increases every time you input the right color value.
teext = f"Points: {points}" # + points
i = 0

def checker(point):
#<--------------PROBLEM HERE--------------->
    point += 1 #this doesn't increase points

while running:
    # close window
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.MOUSEBUTTONDOWN or i == 3:
            #<----------CHECKER CALLED------------>
            checker(points) 
            teext = f"Points: {points}"
            red = random.randrange(256)
            green = random.randrange(256)
            blue = random.randrange(256)
            userred = 0
            usergreen = 0
            userblue = 0 
            i = 0
        if event.type == pygame.KEYDOWN:
            key=pygame.key.name(event.key)
            #concatenate what key was pressed to userred, userblue, etc.
            print(key, "was pressed.")
            if (key == 'up'):
#<--------------PROBLEMS HERE--------------->
                userlist[i] += 1 #this does not work
                userblue += 2 #this does work.
            if (key == 'return'):
                i = i + 1
#vv drawing objects below this

I’m really new to Python(this is my first python project), so I’m unsure as to why the two problems above exist. Any help would be appreciated!

When checker is called, the local name point refers to a number. The line point += 1 is equivalent to point = point + 1, so it makes the local name refer to the result of adding 1 to the current value. That doesn’t affect the variable that was passed it.

userlist = [userred, usergreen, userblue] makes a list with the current values of userred etc., so it’s equivalent to userlist = [0, 0, 0]. Changing userlist[0] doesn’t affect userred and changing userred doesn’t affect userlist[0]. They are unrelated.

2 Likes