My Code is not getting the correct output. Here is the lab assignement : 1.27 LAB: Exact change - functions Define a function called exact_change that takes the total change amount in cents and calculates the change
My Code is not getting the correct output. Here is the lab assignement :
1.27 LAB: Exact change - functions
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 changeEx: If the input is:
45the output is:
2 dimes 1 quarterYour 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_dollars = user_total //100
user_total %= 100
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_dollars, num_quarters, num_dimes, num_nickels, num_pennies)
if __name__ == '__main__':
input_val = int(input())
num_dollars, num_quarters, num_dimes, num_nickels, num_pennies = exact_change(input_val)
if input_val <= 0:
print("no change")
else:
if num_dollars > 1:
print('%d dollars' % num_dollars)
elif num_dollars == 1:
print('%d dollar' % num_dollars)
if num_quarters > 1:
print('%d quarters' % num_quarters)
elif num_quarters == 1:
print('%d quarter' % num_quarters)
if num_dimes > 1:
print('%d dimes' % num_dimes)
elif num_dimes == 1:
print('%d dime' % num_dimes)
if num_nickels > 1:
print('%d nickels' % num_nickels)
elif num_nickels == 1:
print('%d nickel' % num_nickels)
if num_pennies > 1:
print('%d pennies' % num_pennies)
elif num_dimes == 1:
print('%d penny' % num_pennies)
ERRORS ATTACHED
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images