3 Replies - 12000 Views - Last Post: 15 May 2012 - 05:50 PM Rate Topic: -----

#1 alexr1090   User is offline

  • D.I.C Head
  • member icon

Reputation: 44
  • View blog
  • Posts: 126
  • Joined: 08-May 11

Change Return Program Problem

Posted 14 May 2012 - 10:07 PM

I've got a change return program that isn't working correctly but everything looks alright to me. Someone taking a look at it will be appreciated.
[edit]
I've tested it and the problem seems to be converting the floats to ints. The float 1.0 is being converted to 0 as an int. Same with 5.0 being converted to 4 as an int.
[/edit]
#!/usr/bin/python

#Change Return Program
#The user enters a cost and then the amount of money given. The program will figure out the change
#and the number of quarters, dimes, nickels, pennies needed for the change.
def get_cost(): return float(raw_input('Enter Cost:$'))
def get_money(): return float(raw_input('Enter Money Given:$'))
def get_change_left(cost,money_given): return (money_given - cost)
def get_quarters(change_left): 
	quarters=int((change_left*100 / 25))
	change_left -= (quarters * .25)
	return (quarters, change_left)
def get_dimes(change_left): 
	dimes= int((change_left*100 / 10))
	change_left -= (dimes*.10)
	return (dimes, change_left)
def get_nickels(change_left):
	nickels = int((change_left*100 / 5))
	change_left -= (nickels*.05)
	return (nickels,change_left)
def get_pennies(change_left): return int((change_left*100 / 1))
cost = get_cost()
money_given = get_money()
change_left = get_change_left(cost, money_given)
print ("The change is:${:,.2f}").format(change_left)
quarters,change_left = get_quarters(change_left)
dimes, change_left = get_dimes(change_left)
nickels, change_left = get_nickels(change_left)
pennies = get_pennies(change_left)
print ("Change to give\n{:=<14}\n{:<12}{}\n{:<12}{}\n{:<12}{}\n{:<12}{}").format('','Quarters:',quarters,'Dimes:' ,dimes,'Nickels:', nickels,'Pennies:', pennies)


example output:
Enter Cost:$4.95
Enter Money Given:$5
The change is:$0.05
Change to give
==============
Quarters: 0
Dimes: 0
Nickels: 0
Pennies: 4

This post has been edited by alexr1090: 14 May 2012 - 10:16 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Change Return Program Problem

#2 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: Change Return Program Problem

Posted 15 May 2012 - 03:28 AM

Actually, your methodology works VERY well. I was baffled by your problem because when I set change_left equal to 0.05 and did your method, it gave me 5, but when I ran the program, it gave me 4. However, this is because some decimal numbers cannot be accurately displayed in binary form. If you look really closely (without string formatting), the value of change_left does NOT equal 0.05. It equals 4.999999999999982 when multiplied by 100. What you need to do then is to round these to the nearest 2nd decimal. However, that method has unexpected results, so I just rewrote your methods here:

def get_quarters(change_left): 
	quarters=int((round(change_left*100) / 25))
	change_left -= (quarters * .25)
	return (quarters, change_left)
    
def get_dimes(change_left): 
	dimes= int((round(change_left*100) / 10))
	change_left -= (dimes*.10)
	return (dimes, change_left)
    
def get_nickels(change_left):
	nickels = int((round(change_left*100) / 5))
	change_left -= (nickels*.05)
	return (nickels,change_left)
    
def get_pennies(change_left):
    return int(round(change_left*100) / 1)



Also, on issue of style. Python is meant to be pretty. Having NO WHITESPACE is horrible. Spread things out and make it nice and easy to read.

Edit: Also, doing a simple command line test:
>>> x = float(5.0)
>>> y = float(4.95)
>>> x - y
0.04999999999999982



As you can see, this is one case where floats will kill you! The binary representation is right if you do binary subtraction.
Was This Post Helpful? 1
  • +
  • -

#3 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: Change Return Program Problem

Posted 15 May 2012 - 04:09 AM

Yeah, floats will kill you...

Normally I'm a big fan of lots of little methods. However, you're actually making things more complex with this.

I'd just do it all in one and return the data you were planning on formatting:
def parseChange(change):
	pennies = int(change * 100)
	quarters = int(pennies / 25)
	pennies -= quarters * 25
	dimes = int(pennies / 10)
	pennies -= dimes * 10
	nickels = int(pennies / 5)
	pennies -= nickels * 5
	return ('','Quarters:',quarters,'Dimes:' ,dimes,'Nickels:', nickels,'Pennies:', pennies)



Of, maybe a little more pythony:
def parseChange(change):
	result = ['']
	pennies = int(change * 100)
	for name, value in (('Quarters', 25),('Dimes',10),('Nickels', 5),('Pennies', 1)):
		count = int(pennies / value) 
		pennies -= count * value
		result.append(name)
		result.append(count)
	return result


Was This Post Helpful? 1
  • +
  • -

#4 alexr1090   User is offline

  • D.I.C Head
  • member icon

Reputation: 44
  • View blog
  • Posts: 126
  • Joined: 08-May 11

Re: Change Return Program Problem

Posted 15 May 2012 - 05:50 PM

Thanks guys for taking the time to look over the code. It's nice to have a place where people are so willing to help.

dogstopper - thanks for your explanation and solution.

baavgai- After thinking about it, yes I am making things more complicated by having several functions return this change_left value. It would be easier to do it in one. Thanks for the suggested solutions.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1