|
|
Subject:
Python - Basic Program
Category: Computers > Algorithms Asked by: macca111-ga List Price: $25.00 |
Posted:
15 Aug 2006 07:15 PDT
Expires: 14 Sep 2006 07:15 PDT Question ID: 756160 |
I need a Python program that can give the change then break the change into denominations. ie $2, $1, $0.50, $0.20, $0.10, $0.05 | |
| |
|
|
Subject:
Re: Python - Basic Program
Answered By: efn-ga on 27 Aug 2006 10:12 PDT Rated: |
Hi macca111, Here is a program that will do what you have specified. ------------------------------------------------------- # Program to make change, as specified by macca111-ga # Do all arithmetic with integers to avoid the limitations of # floating-point arithmetic. # Read in the inputs. Convert each one to a floating-point number, # multiply by 100 to get cents, and convert to an integer. # Use a named constant instead of a numeric literal. CPD = 100 # Cents per dollar cost = int(CPD * float(raw_input("Enter the cost: "))) tend = int(CPD * float(raw_input("Enter the tendered amount: "))) # Calculate the change. change = tend - cost # Construct a tuple with the possible coin values in cents. denominations = (200, 100, 50, 20, 10, 5) # Get the number of elements in the tuple. ndenominations = len(denominations) # Initialize a list of counts per denomination. coinCount = [] for d in denominations: coinCount.append(0) # To figure out the coins for the change, start with the largest # denomination and work down to the smaller ones. # Keep track of how much change is left to count out in the # "remaining" variable. # For each denomination, as long as remaining is greater than # value of the coin, repeat adding 1 to the coin count for that # denomination and subtracting the denomination value from # remaining. # Initialize remaining to the total amount of change. remaining = change # deno is an index variable that keeps track of the current # denomination. deno = 0 # Loop until either we have given all the change or we have # run out of coin denominations to check. while remaining and deno < ndenominations: # For one denomination, count out coins of that denomination # as long as the remaining amount is greater than the denomination # amount. while remaining >= denominations[deno]: coinCount[deno] += 1 remaining -= denominations[deno] deno += 1 # Report the results. print "Your change is $%.02f"% (float(change) / CPD) for deno in range(0, ndenominations): print "$%02.2f coins:\t" % (float(denominations[deno]) / CPD), coinCount[deno] if remaining: print "Left over:\t$%02.2f" % (float(remaining) / CPD) ------------------------------------------------------- There are, of course, lots of ways this program could be written. If this particular version does not meet your need, please tell me in a request for clarification, and I will revise it for you. Or if you don't understand how some part works, ask for a clarification and I can explain it. Regards, --efn |
macca111-ga
rated this answer:
and gave an additional tip of:
$15.00
Tks. Probably too much work, but very much appreciated. Buy yourself a drink!!! |
|
Subject:
Re: Python - Basic Program
From: beardedcat-ga on 15 Aug 2006 21:03 PDT |
I'm not too familiar with the syntax of python, but what I would do to equate the change is: >>> change = amount_tendered - cost And then for the harder part, the coins: >>> if change >= 2: ... twodollars = floor( change / 2 ) ... newchange = change - ( twodollars * 2 ) >>> if newchange >= 1: ... onedollar = floor( newchange / 1 ) ... newchange = newchange - ( onedollar * 1 ) >>> if newchange >= .50: ... fiftycent = floor( newchange / .50 ) ... newchange = newchange - ( fiftycent * .50 ) >>> if newchange >= .20: ... twentycent = floor( newchange / .20 ) ... newchange = newchange - ( twentycent * .2 ) >>> if newchange >= .10: ... dime = floor( newchange / .10 ) ... newchange = newchange - ( dime * .10) >>> if newchange >= .05: ... nickel = floor( newchange / .05 ) ... newchange = newchange - ( nickel * .05) >>> print "2:" + twodollars >>> print "1:" + onedollar >>> print ".50:" + fiftycent >>> print ".20:" + twentycent >>> print ".10:" + dime >>> print ".05:" + nickel Thats about all I can provide with my knowledge of python. Hope this helps! |
Subject:
Re: Python - Basic Program
From: macca111-ga on 16 Aug 2006 04:19 PDT |
Not quite since I am very new to Python. I am looking at just using calculations and storing them as variables. The if statements seem to complicated and trying to change them to Python is way beyond my scope at this stage. I do however appreciate your comments. |
Subject:
Re: Python - Basic Program
From: macca111-ga on 16 Aug 2006 05:52 PDT |
def main(): cost=input("Enter the cost: ") tend=input("Enter the tendered amount: ") change=tend-cost two=int(change/2) one=int(change-two*2) a=change-two*2-one fifty=int(a/0.5) b=a-fifty*0.5 twenty=int(b/0.20) c=a-fifty*0.5-twenty*0.2 ten=int(c/0.10) d=a-fifty*0.5-twenty*0.2-ten*0.1 five=(d/0.05) print two, one, fifty, twenty, ten, five print "Your change is $%.02f"% change print "$2 coins :",two print "$1 coin :",one print "$0.50 coin :",fifty print "$0.20 :",twenty print "$0.10 :",ten print "$0.05 :",five main() **I came up with this however the last one (five) won't display the same as the others. The Two, One etc all display $. coins : 1, while the five shows $. coins : 1.0 or if I add int before then it only ever shows 0.0. Regards |
Subject:
Re: Python - Basic Program
From: efn-ga on 28 Aug 2006 20:46 PDT |
Thanks for the rating and the tip. |
Subject:
Re: Python - Basic Program
From: arunnair-ga on 04 Sep 2006 10:52 PDT |
Hey guys, You are all making it too complicated. Although the code is a longer and tedious one but gives the perfect result. Here it is def main(): cost = input("Enter the Purchase cost of all items:") tendered = input("Enter the Tendered amount:") change = tendered - cost change = int(change * 100) a = change / 200 print a, " - $2 Coins" b = (change - (a*200)) / 100 print b, " - $1 Coins" c = (change - (a*200) - (b*100)) / 50 print c, " - 50c Coins" d = (change - (a*200) - (b*100) - (c*50)) / 20 print d, " - 20c Coins" e = (change - (a*200) - (b*100) - (c*50) - (d*20)) / 10 print e, " - 10c Coins" f = (change - (a*200) - (b*100) - (c*50) - (d*20) - (e*10)) / 5 print f, " - 5c Coins" main() I hope you guys find it more usefull and simple Regards ArunNair |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
Search Google Answers for |
Google Home - Answers FAQ - Terms of Service - Privacy Policy |