Skip to content

Instantly share code, notes, and snippets.

@wmantly
Created June 23, 2015 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wmantly/0aba3c511a8d05802df6 to your computer and use it in GitHub Desktop.
Save wmantly/0aba3c511a8d05802df6 to your computer and use it in GitHub Desktop.
denominations_USD = [
{ 'title': 'Hundred-dollar bill', 'count': 0, 'val': 10000 },
{ 'title': 'Fifty-dollar bill', 'count': 0, 'val': 5000 },
{ 'title': 'Ten-dollar bill', 'count': 0, 'val': 1000 },
{ 'title': 'Five-dollar bill', 'count': 0, 'val': 500 },
{ 'title': 'One-dollar bill', 'count': 0, 'val': 100 },
{ 'title': 'Quarter', 'count': 0, 'val': 25 },
{ 'title': 'Dime', 'count': 0, 'val': 10 },
{ 'title': 'Nickel', 'count': 0, 'val': 5 },
{ 'title': 'penny', 'count': 0, 'val': 1 }
]
def currency_converter( amount=0, units=[] ):
amount *= 100
for unit in units:
while( amount != 0 and amount >= unit['val'] ):
amount -= unit['val']
unit['count'] += 1
return units
def currency_display( amount=0, units=denominations_USD ):
units = currency_converter( amount , units )
for unit in units:
if unit['count'] == 0: continue
t = [ unit['count'], unit['title'], '\'s' if unit['count'] > 1 else '' ]
print( '{} {}{}'.format(*t) )
return None
currency_amount = float( input('Convert: ') )
currency_display( currency_amount )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment