Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1057421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:51:54+00:00 2026-05-16T17:51:54+00:00

I am making a change program in python. The user must input a dollar

  • 0

I am making a change program in python. The user must input a dollar amount and then the program will calculate the change in twenties, tens, fives, ones, quarters, dimes, nickels, and pennies. I was instructed to use the round function for the pennies because If I input an amount of $58.79, the program tells me to give 3 pennies back when it should be 4. Is there a way to round up these pennies?

I know the value of a penny is .01, but python reads this as .100000000001 which I believe is the problem.

Any help is appreciated, here is the section I need rounded:

# get the amount to change from the user

change = input("Please enter the amount to change: $")

print "To make change for $",change,"give the customer back:"

# calculate number of twenties
twenties = int(change/ 20)

print twenties, "twenties"

change = change - twenties *20

# calculate tens
tens = int(change / 10)

print tens, "tens"

change = change - tens *10

#calculate fives
fives = int(change / 5)

print fives, "fives"

change = change - fives *5

#calculate ones
ones = int(change / 1)

print ones, "ones"

change = change - ones * 1

#calculate quarters
quarters = int(change / .25)

print quarters, "quarters"

change = change - quarters * .25

#calculate dimes
dimes = int(change / .10)

print dimes, "dimes"

change = change - dimes * .10

#calculate nickels
nickels = int(change / .05)

print nickels, "nickels"

change = change - nickels * .05

#calculate pennies
pennies = int(change / .01)

print pennies, "pennies"
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T17:51:55+00:00Added an answer on May 16, 2026 at 5:51 pm

    The problems you are having are a result of imprecise floating-point arithmetic. There is no way to precisely represent 0.01 in IEEE floating point. That is one reason not to use floats when working with currency.

    You should use decimals or even integers, because you know there are at most 2 digits after the decimal point. In that case, just work with the amount in pennies.


    On the problem itself, I think the easiest way to do it is convert your amount in dollars to the amount in pennies, then iterate through a predefined list of values containing listing the equivalent amount of pennies (in descending order) for each denomination:

    def change(amount):
    
        # this can be removed if you pass the amount in pennies
        # rather than dollars
        amount = int(round(amount*100))
    
        values = [2000, 1000, 500, 100, 25, 10, 5, 1]
        denom = ['twenties', 'tens', 'fives', 'ones', 'quarters', 'dimes', 'nickels', 'pennies']
    
        for i in range(len(values)):
            num = amount / values[i]
            amount -= num * values[i]
            print str(num) + " " + denom[i]
    

    Now calling change(58.79) will print

    2 twenties
    1 tens
    1 fives
    3 ones
    3 quarters
    0 dimes
    0 nickels
    4 pennies
    

    As seen on codepad.org

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

working on making a flash program that loads about 1000 jpegs and then plays
Now I'm making a little program in Java which must read a really big
I'm making a relatively simple program which will also be running on a few
I'm making a java command line program. How can I change the contents of
I'm currently making a whimsical iPhone app that will allow you to change your
I'm making a program that fits the wizard concept ideally; the user is walked
Possible Duplicate: How to change format of date/time? In my program I'm making an
I'm making an application that will change position of two characters in Word. Imports
I am making a python program, and I want to check if it is
I'm making a timecard program, where the user enters the start time, end time,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.