1

and I'm trying to figure out how I would go about passing local variables to a function and then returning the modified values. I've written the code below:

def main():
    change = 150    
    coins = 0
    quarter = 25

    while (change >= quarter):
        change = change - quarter
        coins += 1
    print(coins)

if __name__ == "__main__":
    main()

But I'd like to be able to extract the modification of the change and coins variables like so:

def main():
    change = 150    
    coins = 0
    quarter = 25

    while (change >= quarter):
        count (change, coins, quarter)

def count(change, count, n):
    change = change - n
    count += 1
    return change, count

if __name__ == "__main__":
    main()

However, I know this isn't the way to do it. From what I understand, there could be an issue with trying to return multiple variables from the function, but it also seems like there an issue when I try to even modify only the change variable within the count function. I would really appreciate any advice.

1 Answer 1

3

You're returning two values from count(), so you should capture those values when you call it:

while (change >= quarter):
    change, coins = count(change, coins, quarter)

Modifying ch and co inside count() will not affect the outer values change and coins.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.