0% found this document useful (1 vote)
2K views2 pages

CYB-130 8.26 LAB: Exact Change - Functions

The document describes a Python 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 the amounts of dollars, quarters, dimes, nickels, and pennies, and a main function that gets user input, calls exact_change, and prints the results in a readable format.

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 (1 vote)
2K views2 pages

CYB-130 8.26 LAB: Exact Change - Functions

The document describes a Python 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 the amounts of dollars, quarters, dimes, nickels, and pennies, and a main function that gets user input, calls exact_change, and prints the results in a readable format.

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

CYB-130

8.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.
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