J01
J01

Reputation: 145

Problem updating variable in main function after calculations in another function

Basically, I have a list of numbers as a variable in my main function. I call another function to temporarily combine that list with another list into a dictionary to perform calculations. Then, I split the dictionary back into lists and return the variables. For some reason, the variable y didn't update in the main function

def main():
    
    x = 'ABC'
    y = [1,1,1]
    
    while True:
        print(y)
        reply = input()

        a_function(x, y)
    
        if reply == '':
            break

def a_function(x, y):
    
    dictionary = dict(zip(x, y))
    
    dictionary['A'] += 1
    
    y = dictionary.values()
    
    return y

main()

Current Output: y = [1,1,1], y = [1,1,1]....

Expected Output: y = [2,1,1], y = [3,1,1], y = [4,1,1]

Upvotes: 0

Views: 28

Answers (1)

cookesd
cookesd

Reputation: 1336

You aren't setting the return value from a_function to anything in main so the values in main stay the same. In main you need to change your call to a_function to:

y = a_function(x,y)

for the value of y in main to update.

Upvotes: 1

Related Questions