Coin Change Maker Python Program
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!
python
add a comment |
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!
python
There's an interesting wikipedia page on this algorithm: en.wikipedia.org/wiki/Change-making_problem.
– NightShadeQueen
Aug 31 '15 at 18:30
1
The tricky part is to know that you by taking as many quarters as possible without exceeding the sum, then as many dimes and then nickels and last pennies will actually result in the minimal amount of coins. And of course you should present the minimal amount or you could just anser to usecpennies.
– skyking
Aug 31 '15 at 18:32
add a comment |
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!
python
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!
python
python
edited Aug 31 '15 at 18:10
Eric Renouf
11.1k32349
11.1k32349
asked Aug 31 '15 at 18:06
bulsona15bulsona15
813
813
There's an interesting wikipedia page on this algorithm: en.wikipedia.org/wiki/Change-making_problem.
– NightShadeQueen
Aug 31 '15 at 18:30
1
The tricky part is to know that you by taking as many quarters as possible without exceeding the sum, then as many dimes and then nickels and last pennies will actually result in the minimal amount of coins. And of course you should present the minimal amount or you could just anser to usecpennies.
– skyking
Aug 31 '15 at 18:32
add a comment |
There's an interesting wikipedia page on this algorithm: en.wikipedia.org/wiki/Change-making_problem.
– NightShadeQueen
Aug 31 '15 at 18:30
1
The tricky part is to know that you by taking as many quarters as possible without exceeding the sum, then as many dimes and then nickels and last pennies will actually result in the minimal amount of coins. And of course you should present the minimal amount or you could just anser to usecpennies.
– skyking
Aug 31 '15 at 18:32
There's an interesting wikipedia page on this algorithm: en.wikipedia.org/wiki/Change-making_problem.
– NightShadeQueen
Aug 31 '15 at 18:30
There's an interesting wikipedia page on this algorithm: en.wikipedia.org/wiki/Change-making_problem.
– NightShadeQueen
Aug 31 '15 at 18:30
1
1
The tricky part is to know that you by taking as many quarters as possible without exceeding the sum, then as many dimes and then nickels and last pennies will actually result in the minimal amount of coins. And of course you should present the minimal amount or you could just anser to use
c pennies.– skyking
Aug 31 '15 at 18:32
The tricky part is to know that you by taking as many quarters as possible without exceeding the sum, then as many dimes and then nickels and last pennies will actually result in the minimal amount of coins. And of course you should present the minimal amount or you could just anser to use
c pennies.– skyking
Aug 31 '15 at 18:32
add a comment |
3 Answers
3
active
oldest
votes
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
Thank you so much for your response! I appreciate the help! I can see where I am going wrong and I now know how to fix it!
– bulsona15
Aug 31 '15 at 18:32
no problem, just make sur you understand the % and // operators. that's the whole point of this exercice.
– Saimu
Aug 31 '15 at 18:34
2
There's alsodivmod, which does both of those operations at the same time.
– NightShadeQueen
Aug 31 '15 at 18:35
add a comment |
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)
add a comment |
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 :).
Thank you for your help! I am definitely trying to make things harder than they really are! I appreciate the help!
– bulsona15
Aug 31 '15 at 18:33
No it's ok. If you just started I understand :) .you are welcome ;)
– Hybr1d
Aug 31 '15 at 18:47
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f32317241%2fcoin-change-maker-python-program%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
Thank you so much for your response! I appreciate the help! I can see where I am going wrong and I now know how to fix it!
– bulsona15
Aug 31 '15 at 18:32
no problem, just make sur you understand the % and // operators. that's the whole point of this exercice.
– Saimu
Aug 31 '15 at 18:34
2
There's alsodivmod, which does both of those operations at the same time.
– NightShadeQueen
Aug 31 '15 at 18:35
add a comment |
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
Thank you so much for your response! I appreciate the help! I can see where I am going wrong and I now know how to fix it!
– bulsona15
Aug 31 '15 at 18:32
no problem, just make sur you understand the % and // operators. that's the whole point of this exercice.
– Saimu
Aug 31 '15 at 18:34
2
There's alsodivmod, which does both of those operations at the same time.
– NightShadeQueen
Aug 31 '15 at 18:35
add a comment |
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
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 Aug 31 '15 at 18:26
SaimuSaimu
244139
244139
Thank you so much for your response! I appreciate the help! I can see where I am going wrong and I now know how to fix it!
– bulsona15
Aug 31 '15 at 18:32
no problem, just make sur you understand the % and // operators. that's the whole point of this exercice.
– Saimu
Aug 31 '15 at 18:34
2
There's alsodivmod, which does both of those operations at the same time.
– NightShadeQueen
Aug 31 '15 at 18:35
add a comment |
Thank you so much for your response! I appreciate the help! I can see where I am going wrong and I now know how to fix it!
– bulsona15
Aug 31 '15 at 18:32
no problem, just make sur you understand the % and // operators. that's the whole point of this exercice.
– Saimu
Aug 31 '15 at 18:34
2
There's alsodivmod, which does both of those operations at the same time.
– NightShadeQueen
Aug 31 '15 at 18:35
Thank you so much for your response! I appreciate the help! I can see where I am going wrong and I now know how to fix it!
– bulsona15
Aug 31 '15 at 18:32
Thank you so much for your response! I appreciate the help! I can see where I am going wrong and I now know how to fix it!
– bulsona15
Aug 31 '15 at 18:32
no problem, just make sur you understand the % and // operators. that's the whole point of this exercice.
– Saimu
Aug 31 '15 at 18:34
no problem, just make sur you understand the % and // operators. that's the whole point of this exercice.
– Saimu
Aug 31 '15 at 18:34
2
2
There's also
divmod, which does both of those operations at the same time.– NightShadeQueen
Aug 31 '15 at 18:35
There's also
divmod, which does both of those operations at the same time.– NightShadeQueen
Aug 31 '15 at 18:35
add a comment |
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)
add a comment |
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)
add a comment |
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)
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)
edited Aug 31 '15 at 19:21
answered Aug 31 '15 at 19:14
NightShadeQueenNightShadeQueen
2,44331532
2,44331532
add a comment |
add a comment |
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 :).
Thank you for your help! I am definitely trying to make things harder than they really are! I appreciate the help!
– bulsona15
Aug 31 '15 at 18:33
No it's ok. If you just started I understand :) .you are welcome ;)
– Hybr1d
Aug 31 '15 at 18:47
add a comment |
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 :).
Thank you for your help! I am definitely trying to make things harder than they really are! I appreciate the help!
– bulsona15
Aug 31 '15 at 18:33
No it's ok. If you just started I understand :) .you are welcome ;)
– Hybr1d
Aug 31 '15 at 18:47
add a comment |
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 :).
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 Aug 31 '15 at 18:26
Hybr1dHybr1d
85110
85110
Thank you for your help! I am definitely trying to make things harder than they really are! I appreciate the help!
– bulsona15
Aug 31 '15 at 18:33
No it's ok. If you just started I understand :) .you are welcome ;)
– Hybr1d
Aug 31 '15 at 18:47
add a comment |
Thank you for your help! I am definitely trying to make things harder than they really are! I appreciate the help!
– bulsona15
Aug 31 '15 at 18:33
No it's ok. If you just started I understand :) .you are welcome ;)
– Hybr1d
Aug 31 '15 at 18:47
Thank you for your help! I am definitely trying to make things harder than they really are! I appreciate the help!
– bulsona15
Aug 31 '15 at 18:33
Thank you for your help! I am definitely trying to make things harder than they really are! I appreciate the help!
– bulsona15
Aug 31 '15 at 18:33
No it's ok. If you just started I understand :) .you are welcome ;)
– Hybr1d
Aug 31 '15 at 18:47
No it's ok. If you just started I understand :) .you are welcome ;)
– Hybr1d
Aug 31 '15 at 18:47
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f32317241%2fcoin-change-maker-python-program%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
There's an interesting wikipedia page on this algorithm: en.wikipedia.org/wiki/Change-making_problem.
– NightShadeQueen
Aug 31 '15 at 18:30
1
The tricky part is to know that you by taking as many quarters as possible without exceeding the sum, then as many dimes and then nickels and last pennies will actually result in the minimal amount of coins. And of course you should present the minimal amount or you could just anser to use
cpennies.– skyking
Aug 31 '15 at 18:32