Michael Stewart
Michael Stewart

Reputation: 9

Can not get variables to pass from function to main

I can not get main to print anything besides the variables are equal to zero which means my greedy1 function is not passing my variable and I do not know what is wrong.

def greedy1(total):

if (total >= 25):
    q = total // 25
    remainderCentsQ = total % 25
    d = remainderCentsQ // 10
    remainderCentsD = total % 10
    n = remainderCentsD // 5
    remainderCentsN = total % 5
    p = remainderCentsN // 1
    return (q, d, n, p)

elif (total >= 10):
    q = 0
    d = total // 10
    remainderCentsD = total % 10
    n = remainderCentsD // 5
    remainderCentsN = total % 5
    p = remainderCentsN // 1
    return (q, d, n, p)
elif (total >= 5):
    q = 0
    d = 0
    n = total // 5
    remainderCentsN = total % 5
    p = remainderCentsN // 1
    return (q, d, n, p)
else:
    q = 0
    d = 0
    n = 0
    p = total // 1
    return (q, d, n, p)

def printCoins1(total, q, d, n, p):
    print("For " + str(total) + " cents change, I give\n" + str(q) + " quarters\n" + str(d) + " dimes\n" + str(n) + " nickels\n" + str(p) + " pennies")


def main():
    total = int(input("How many cents change? "))
    q = 0
    d = 0
    n = 0
    p = 0
    greedy1(total)
    printCoins1(total, q, d, n, p)

main()

Upvotes: 0

Views: 30

Answers (2)

cpt.John
cpt.John

Reputation: 173

@Sayas has given the correct answer however there is some redundant code , so I will improve it in my answer

def main():
    total = int(input("How many cents change? "))
    printCoins1(total, *greedy1(total))

Upvotes: 0

Sayas
Sayas

Reputation: 37

Inside your main function, you're calling greedy1 function with total that returns the tuple. But you're not storing this return values back to your variables q, d, n, p.

your main() should look like this:

def main():
    total = int(input("How many cents change? "))

    (q, d, n, p) = greedy1(total)
    printCoins1(total, q, d, n, p)

Upvotes: 3

Related Questions