0% found this document useful (3 votes)
7K views

5.23 LAB Exact Change - Functions

The document describes a program that takes a total change amount as input and outputs the minimum number of coins needed to make that change. It defines a function called "exact_change" that calculates and returns the number of dollars, quarters, dimes, nickels, and pennies. The main part of the program calls this function, and prints the number and type of each coin required to make the change.

Uploaded by

CHRIS D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (3 votes)
7K views

5.23 LAB Exact Change - Functions

The document describes a program that takes a total change amount as input and outputs the minimum number of coins needed to make that change. It defines a function called "exact_change" that calculates and returns the number of dollars, quarters, dimes, nickels, and pennies. The main part of the program calls this function, and prints the number and type of each coin required to make the change.

Uploaded by

CHRIS D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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