Coin Change Maker Python Program

Question:

I am in a beginner programming course. We must do an exercise where we make a change maker program. The input has to be between 0-99 and must be represented in quarters, dimes, nickles, and pennies when the input is divided down between the four. I wrote a code that involved loops and whiles, but he wants something more easy and a smaller code. He gave me this as a way of helping me along:

c=int(input('Please enter an amount between 0-99:'))
print(c//25)
print(c%25)

He told us that this was basically all we needed and just needed to add in the dimes, nickles, and pennies. I try it multiple ways with the dimes, nickles, and pennies, but I cannot get the output right. Whenever I enter ’99’, I get 3 for quarters, 2 for dimes, 1 for nickles, and 0 for pennies. If anyone would be able to help me, that would be wonderful!

Asked By: bulsona15

||

Answers:

n = int(input("Enter a number between 0-99"))
q = n // 25
n %= 25
d = n // 10
n %= 10
ni =  n // 5
n %= 5
c = n % 5
print(str(q) +" " + str(d) +" " + str(ni) + " " + str(c))

I hope this helps? Something like this but don’t just copy it. Everytime you divide by 25 10 5 you must lose that part because it’s already counted.At the end print what ever you want :).

Answered By: Hybr1d

I’m now sure about what you want to achieve. Using the modulo operator you could easily find out how many quarters, dimes, nickles and pennies.

Let’s just say you input 99.

c=int(input('Please enter an amount between 0-99:'))
print(c//25, "quarters")
c = c%25
print(c//10, "dimes")
c = c%10
print(c//5, "nickles")
c = c%5
print(c//1, "pennies")

this would print out:

3 quarters
2 dimes
0 nickles
4 pennies
Answered By: Saimu

The actual trick is knowing that because each coin is worth at least twice of the next smaller denomination, you can use a greedy algorithm. The rest is just implementation detail.

Here’s a slightly DRY’er (but possibly, uh, more confusing) implementation. All I’m really doing differently is using a list to store my results, and taking advantage of tuple unpacking and divmod. Also, this is a little easier to extend in the future: All I need to do to support $1 bills is to change coins to [100, 25, 10, 5, 1]. And so on.

coins = [25,10,5,1] #values of possible coins, in descending order
results = [0]*len(coins) #doing this and not appends to make tuple unpacking work
initial_change = int(input('Change to make: ')) #use raw_input for python2
remaining_change = initial_change
for index, coin in enumerate(coins):
    results[index], remaining_change = divmod(remaining_change, coin)
print("In order to make change for %d cents:" % initial_change)
for amount, coin in zip(results, coins):
    print("    %d %d cent piece(s)" % (amount, coin))

Gives you:

Change to make: 99
In order to make change for 99 cents:
    3 25 cent piece(s)
    2 10 cent piece(s)
    0 5 cent piece(s)
    4 1 cent piece(s)
Answered By: NightShadeQueen
""" 
  Change Machine - Made by A.S Gallery

  This program shows the use of modulus and integral division to find the quarters, nickels, dimes, pennies of the user change !!
  Have Fun Exploring !!!

"""

#def variables
user_amount = float(input("Enter the amount paid : "))
user_price = float(input("Enter the price : "))


# What is the change ?? (change calculation)
user_owe = user_amount - user_price

u = float(user_owe)

print "Change owed : " + str(u)


"""
    Calculation Program (the real change machine !!)

"""


# Variables for Calculating Each Coin !!
calculate_quarters = u//.25

# Using the built-in round function in Python !!
round(calculate_quarters)

print "Quarters : " + str(calculate_quarters)

u = u%0.25

calculate_dime = u//.10

round(calculate_dime)

print "Dime : " + str(calculate_dime)

u = u%0.10

calculate_nickels = u//.05  

round(calculate_nickels)

print "Nickels : " + str(calculate_nickels)

u = u%0.5

calculate_pennies = u//.01

round(calculate_pennies)

print "Pennies : " + str(calculate_pennies

Code for the change machine works 100%, its for CodeHs Python

Answered By: A.S Gallery

This is probably one of the easier ways to approach this, however, it can also
be done with less repetition with a while loop

cents = int(input("Input how much money (in cents) you have, and I will tell 
you how much that is is quarters, dimes, nickels, and pennies. "))

quarters = cents//25
quarters_2 = quarters*25
dime = (cents-quarters_2)//10
dime_2 = dime*10
nickels = (cents-dime_2-quarters_2)//5
nickels_2 = nickels*5
pennies = (cents-dime_2-quarters_2-nickels_2)
Answered By: OliviaM

Here’s a Python function that takes an input between 0 and 99 (inclusive) and returns the number of quarters, dimes, nickels, and pennies needed to represent that amount.

def calculate_coins(amount):
if amount < 0 or amount > 99:
    return "Error: Input must be between 0 and 99 (inclusive)"

quarters = amount // 25
amount %= 25

dimes = amount // 10
amount %= 10

nickels = amount // 5
amount %= 5

pennies = amount

return (quarters, dimes, nickels, pennies)
>>> calculate_coins(87)
(3, 1, 0, 2)

you’re welcome 🙂 7 years from the future.

Answered By: blackline
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.