7

Hi everyone I am currently doing a school project and even my teacher is stumped. In Canada the penny has been removed so now all purchases are rounded to either 0 or 5. For example 5.53 would become 5.55 and 5.52 would become 5.50. I am trying to get my program to round like this, but I can't figure out how. I know how to round to decimal places, but I don't know how to round to specifics like this. Any help would be appreciated!

Here is my code. The project is about making a program that a cashier would use in a coffee shop.

order = ['coffee', 'tea', 'hashbrown','jelly','cream','chocolate','glazed','sandwich','bagel','cookie','pannini']
quantity = ['0','0','0','0','0','0','0','0','0','0','0']

# coffee = $1 
# Tea = $1.30 
# hashbrown = $1.25 
# all donuts = $1.50 
# sandwich = $2.50 
# bagel = $2 
# cookie = $0.50 
# pannini = $4

cashier = 1
total = 0

while cashier == 1:
    print "What did the customer order?"
    ordered = input ()

    while ordered > 10 or ordered < 0:
        print "Do you want to input a valid order?"
        ordered = input ()



    print "How many are being ordered?"
    quantityorder = input ()
    quantity[ordered] = quantityorder
    print "Ordered",quantityorder,"",order[ordered],"!"  
    if ordered == 0:
        ordered = 1.0
    elif ordered == 1:
        ordered = 1.30
    elif ordered == 2:
        ordered = 1.25
    elif ordered == 3 or ordered == 4 or ordered == 5 or ordered == 6:
        ordered = 1.50
    elif ordered == 7:
        ordered = 2.50
    elif ordered == 8:
        ordered = 2
    elif ordered == 9:
        ordered = 0.50
    else:
        ordered = 4.0


    price = ordered * quantityorder
    total = total + price
    print "Anything else?"
    cashier = input ()  #If the user inputs 1 then they can input another order if they didn't put in 1 then the program assumes that it is the end of a customers order



print "Your total is $", total * 1.13,"!"
total = total * 1.13
print
print "How much money was given?"
print
money = input ()* 1.0
while money < total:
    print "Please input a valid number!"
    money = input ()

print "The change should be $",money - total,"!"
4
  • 2
    Provide us with some code. Keep in mind that your program could be handling the numbers as numbers or strings and the best way to handle it is different, depending on how the program treats your numbers. Give us the code and you give us the context to help us give you a meaninful answer!
    – jwarner112
    Dec 11, 2013 at 15:58
  • 2
    Double it, round, then halve it.
    – wim
    Dec 11, 2013 at 15:59
  • 1
    The currently highest-voted answer (by @haael, relying on quantize) is completely wrong. I don't have enough time at the moment to really work out a robust solution, but this answer from a related question is a much better approach. Yes, it's float, which is what I would have to check on, but at least it could be adapted to Decimal if needed.
    – John Y
    Jun 15, 2014 at 22:10
  • Also, for what it's worth, @wim's comment contains a working approach. It could be fleshed out into an answer.
    – John Y
    Jun 15, 2014 at 22:17

7 Answers 7

1

This tortured me until I solved it. One rule I set for myself was NOT to use a case-by-case switch on modulo 5, but to use builtin functions. Nice puzzle. wim's "double it, round, then halve it" comment was close.

def nickelround(n):
    N = int(n)
    print "%0.2f" % (N + round((n - N) * 20) * 0.05)
0
In [42]:
x=0.57
int(x/0.05)*0.05

Out[42]:
0.55
0
0

Usually in these programming problems you're explicitly asked to solve for how to "make change", not just provide the total amount of change due (which is trivial). So you can just in-line the pennies correction into that function:

def make_change(bal):
    bal = bal + .02 #correction for no pennies
    currency = [20,10,5,1,.25,.1,.05]
    change = {}
    for unit in currency:
        change[unit] = int(bal // unit)
        bal %= unit
    return change

This particular form returns a dict of change denominations and their corresponding counts.

0

As you already hinted, use the Decimal class (module decimal). Replace all floats in your code with decimal constructors like Decimal('13.52').

Now the function you want to use is 'quantize'.

You use it like that:

Decimal('1.63').quantize(Decimal('0.5'), rounding=ROUND_HALF_DOWN)

The parameter Decimal('0.5') indicates that you want to round to the halves.

UPDATE: As the OP wanted to round to the units of 0.05, he must obviously use:

Decimal('1.63').quantize(Decimal('0.05'), rounding=ROUND_HALF_DOWN)
4
  • Ya if it was 1.63 I would want it to round to 1.65
    – CF11
    Dec 11, 2013 at 16:11
  • Use quantize(Decimal('0.05')) then.
    – haael
    Dec 11, 2013 at 16:13
  • 1
    How do you get the 1.65 out of this? When I try it, it seems to remain at 1.63.
    – wim
    Dec 11, 2013 at 19:50
  • 3
    This is a completely wrong answer. It totally misses how quantize works and doesn't do what is asked by the question. It is mystifying how it has any upvotes at all, let alone 4.
    – John Y
    Jun 15, 2014 at 22:04
0

I'm going to be old-school and say, convert your number into a list of values with a null or some kind of tag to represent the decimal.

The make a variable to hold your rounded number and add each value from 1000s or whatever, to 100s, 10s and 1s. (You are looping of course).

Once your loop hits the tag, depending on how far you want to round, (this should be a parameter for your function) throw in a conditional to represent how you want to round ie. 5 means round up, 4 round down (this is another parameter). Then add the final value(s) and return your newly rounded number.

Step 2: Fire your teacher because this is an unbelievably easy problem.

Bare in mind, anyone who knows c or has worked with memory assignment should find this question a breeze.

0

You could use something as simple as this to round your numbers to a given base:

def round_func(x, base=0.05):
    return round(base*round(float(x)/base), 2)

And since you want only 2 decimal places, you can just round it to 2 decimals. The inner round function is to round the given number as per the base, which is 0.05 in your case.

0

Do you know, how to use own functions? Add this function to your code:

def nickelround(m):
  return round(m/0.05,0)*0.05

and use it:

print "The change should be $", nickelround(money - total),"!" 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.