'Why can I not alter the value of shape_amount?

I am struggling to find a solution to add 1 to the value of shape_amount within a different class and return the value back to compare it.

This is the first class that compares the value of shape_amount.

class BEGIN_DRAWING:
    def begin_draw(run, shape_opt, color_value, color):
        global shape_amount
        shape_amount = 0
        i = True
        while i == True:
            color = color_value(color)
            run.movement(shape_amount, color)
            print(shape_amount)
            if shape_amount == shape_opt:
                i = False
        run.movement(shape_amount, color)

I print shape_amount to check its values but outputs 0

class DRAWING_CURVED:
    def fill(shape_amount, color):
        rand_int = randint(0, 20)
        if rand_int == 0:
            end_fill()
            fillcolor(color)
            begin_fill()
            shape_amount += 1
            return shape_amount 

This is supposed to add 1 to the value of shape_amount every time a shape is filled but it does not seem to be doing so.



Solution 1:[1]

From what I can tell, you are passing the shape_amount global variable into a function as a parameter. This parameter will not be treated as the global variable, but as a normal parameter.

The below highlights this, it prints for me:
2
1
2

def test():
    global x
    x = 1
    val = increment(x)
    print(val)
    print(x)
    increment_two()
    print(x)

def increment(x):
    return x + 1

def increment_two():
    global x
    x += 1

if __name__ == "__main__":
    test()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tytrox