Advertisement
Quoteory

Python Coin Script

Sep 9th, 2019
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.78 KB | None | 0 0
  1. change_types = {25: "Quarter", 10: "Dime", 5: "Nickel", 1: "Penny"}
  2.  
  3. def change(cents): # returns change in coins
  4.     coin_values = [25, 10, 5, 1]
  5.     coins = {25: 0, 10: 0, 5: 0, 1: 0}
  6.  
  7.     def findNext(): # finds next largest coin type
  8.         for coin_val in coin_values:
  9.             if cents >= coin_val: return coin_val
  10.  
  11.     while cents > 0: # repeats adding coins until 0 cents
  12.         nextCoin = findNext()
  13.         if nextCoin:
  14.             cents -= nextCoin
  15.             coins[nextCoin] += 1
  16.  
  17.     return coins
  18.  
  19. def change_text(change_dic): # makes coin dictionary look nicer
  20.     new_dic = {}
  21.     for coin_value, coin_quantity in change_dic.items():
  22.         new_dic[change_types[coin_value]] = coin_quantity
  23.     return new_dic
  24.  
  25. coins = change(55)
  26. print(change_text(coins))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement