5.23 LAB Exact Change - Functions

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

5.

23 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)
Note: This is a lab from a previous chapter that now requires the use of a function.
def exact_change(user_total):

dollars = user_total // 100

user_total %= 100

quarters = user_total // 25

user_total %= 25

dimes = user_total // 10

user_total %= 10

nickels = user_total // 5

user_total %= 5

pennies = user_total

return dollars, quarters, dimes, nickels, pennies

def main():

total = int(input())

if total <= 0:
print('no change')

else:

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

if dollars > 1:

print('%d dollars' % dollars)

elif dollars == 1:

print('%d dollar' % dollars)

if quarters > 1:

print('%d quarters' % quarters)

elif quarters == 1:

print('%d quarter' % quarters)

if dimes > 1:

print('%d dimes' % dimes)

elif dimes == 1:

print('%d dime' % dimes)

if nickels > 1:

print('%d nickels' % nickels)

elif nickels == 1:

print('%d nickel' % nickels)

if pennies > 1:

print('%d pennies' % pennies)

elif pennies == 1:

print('%d penny' % pennies)

if __name__ == '__main__':

main()

You might also like