[Tutor] Ingenious script (IMO)

Jeff Johnson jeff at san-dc.com
Sun Aug 12 03:39:58 CEST 2007


I wish I had a better way to represent "pennies" vs. "penny" but it works.
This was a nice challenge to help me learn python.  Thanks and keep them
coming!

My contribution:

cents = 100

cost = int(round(cents * float(raw_input("Enter the cost: "))))
tendered = int(round(cents * float(raw_input("Enter the amount tendered:
"))))

change = tendered - cost

denominations = [2000, 1000, 500, 100, 50, 25, 10, 5, 1]
money = ['$20.00', '$10.00', '$5.00', '$1.00', 'Fifty Cent Piece',
'Quarter', 'Dime', 'Nickel', 'Penny']
moneys = ['$20.00', '$10.00', '$5.00', '$1.00', 'Fifty Cent Pieces',
'Quarters', 'Dimes', 'Nickels', 'Pennies']

givetocustomer = [0, 0, 0, 0, 0, 0, 0, 0, 0]

lnlen = len(denominations)
    
lni = 0
while lni <= lnlen - 1:
  # floor division yields no decimals and no rounding
  givetocustomer[lni] = change // denominations[lni]
  # modulus returns remainder
  change = change % denominations[lni]
  if givetocustomer[lni] > 0:
    if givetocustomer[lni] > 1:
      print str(givetocustomer[lni]) + ' ' + moneys[lni]
    else:
      print str(givetocustomer[lni]) + ' ' + money[lni]
  lni += 1

Jeff

Jeff Johnson
jeff at san-dc.com
623-582-0323
Fax 623-869-0675

> -----Original Message-----
> From: tutor-bounces at python.org [mailto:tutor-bounces at python.org] On Behalf
> Of Dick Moores
> Sent: Monday, August 06, 2007 6:12 AM
> To: Python Tutor List
> Subject: [Tutor] Ingenious script (IMO)
> 
> Google Answers folded, but Google has kept the archive accessible. I
> found this Python script at
> <http://answers.google.com/answers/threadview?id=756160>
> 
> and modified it for U.S. money denominations:
> 
> http://www.rcblue.com/Python/changeMaker_revised_for_US_denominations.py
> 
> I'm still working at Python--been at it a while--and thought the
> script was ingenious. Do the Tutors agree? Or is it just
> run-of-the-mill programming? Could it have been more simply written?
> 
> Thanks,
> 
> Dick Moores
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> No virus found in this incoming message.
> Checked by AVG Free Edition.
> Version: 7.5.476 / Virus Database: 269.11.6/938 - Release Date: 8/5/2007
> 4:16 PM
> 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 4232 bytes
Desc: not available
Url : http://mail.python.org/pipermail/tutor/attachments/20070811/f0295dbb/attachment.bin 


More information about the Tutor mailing list