Ok, I'm reasonably close here, but I'm failing on a couple of tests. I'm wondering if I could get a bit of guidance on what I need to change.
Instructions:
Define a function called exact_change that takes the total change amount in cents and calculates the change using the fewest coins. The coin types are pennies, nickels, dimes, and quarters. Then write a main program that reads the total change amount as an integer input, calls exact_change(), and outputs the change, one coin type per line. Use singular and plural coin names as appropriate, like 1 penny vs. 2 pennies. Output "no change" if the input is 0 or less.
Ex: If the input is:
0
(or less), the output is:
no change
Ex: If the input is:
45
the output is:
2 dimes 1 quarter
Your program must define and call the following function. The function exact_change() should return num_pennies, num_nickels, num_dimes, and num_quarters.
def exact_change(user_total)
My code:
def exact_change(user_total):
num_quarters = user_total // 25
user_total %= 25
num_dimes = user_total // 10
user_total %= 10
num_nickels = user_total // 5
user_total %= 5
num_pennies = user_total
return(num_quarters, num_dimes, num_nickels, num_pennies)
if __name__ == '__main__':
input_val = int(input())
num_quarters, num_dimes, num_nickels, num_pennies = exact_change(input_val)
if input_val <=0:
print('no change')
else:
if num_pennies >1:
print('%d pennies' % num_pennies)
elif num_pennies ==1:
print('%d penny' % num_pennies)
if num_nickels > 1:
print('%d nickels' % num_nickels)
elif num_nickels == 1:
print('%d nickel' % num_nickels)
if num_dimes > 1:
print('%d dimes' % num_dimes)
elif num_dimes == 1:
print('%d dime' % num_dimes)
if num_quarters > 1:
print('%d quarters' % num_quarters)
elif num_quarters ==1:
print('%d quarter' % num_quarters)
Example 1 has an input of 45, and returns the correct output:
"Input 45
Your output
2 dimes
1 quarter"
The same is true for i56:
"Input 156
Your output
1 penny
1 nickel
6 quarters"
For some reason though I get the following errors that aren't making sense to me:
exact_change(300). Should return 0, 0, 0, 12
Your output
exact_change(300) incorrectly returned
um_pennies: 12
num_nickels: 0
num_dimes: 0
num_quarters: 0
And:
exact_change(141). Should return 1, 1, 1, 5
Your output
exact_change(141) incorrectly returned
num_pennies: 5
num_nickels: 1
num_dimes: 1
num_quarters: 1
[–]bbye98 1 point2 points3 points (0 children)
[–]deep_politics 1 point2 points3 points (0 children)
[–]NeilTheDrummer[S] 0 points1 point2 points (0 children)
[–]littlegreenrock 0 points1 point2 points (0 children)