5.2k views
2 votes
6.26 LAB: Exact change - functions

Write a program with total change amount as an integer input that outputs the change using the fewest coins, one coin type per line. The coin types are dollars, quarters, dimes, nickels, and pennies. Use singular and plural coin names as appropriate, like 1 penny vs. 2 pennies.

Ex: If the input is:

0
or less, the output is:

no change
Ex: If the input is:

45
the output is:

1 quarter
2 dimes
Your program must define and call the following function. The function exact_change() should return num_dollars, num_quarters, num_dimes, num_nickels, and num_pennies.
def exact_change(user_total)

User Trevorade
by
8.3k points

1 Answer

3 votes

Answer:

Certainly! Here's a Python program that fulfills the given requirements using the exact_change() function:

Step-by-step explanation:

def exact_change(user_total):

if user_total <= 0:

print("no change")

else:

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

# Taking user input for the total change amount

change_amount = int(input())

# Calling the function and receiving the returned values

dollars, quarters, dimes, nickels, pennies = exact_change(change_amount)

# Displaying the output

if dollars > 0:

if dollars == 1:

print(f"{dollars} dollar")

else:

print(f"{dollars} dollars")

if quarters > 0:

if quarters == 1:

print(f"{quarters} quarter")

else:

print(f"{quarters} quarters")

if dimes > 0:

if dimes == 1:

print(f"{dimes} dime")

else:

print(f"{dimes} dimes")

if nickels > 0:

if nickels == 1:

print(f"{nickels} nickel")

else:

print(f"{nickels} nickels")

if pennies > 0:

if pennies == 1:

print(f"{pennies} penny")

else:

print(f"{pennies} pennies")

User Gonzofish
by
8.9k points