|
| 1 | +# Lab 06: Make Change |
| 2 | + |
| 3 | +# Let's convert a dollar amount into a number of coins. The input will be the total amount, the output will be the number of quarters, dimes, nickles, and pennies. |
| 4 | +# Always break the total into the highest coin value first, resulting in the fewest amount of coins. For this, you'll have to use floor division //, |
| 5 | +# which throws away the remainder. 10/3 is 3.333333, 10//3 is 3. |
| 6 | + |
| 7 | +# Welcome to the Change Maker 5000 (tm) |
| 8 | +# Enter a dollar amount: 1.36 |
| 9 | +# 5 quarters, 1 dime, and 1 penny |
| 10 | +# Would you like make more change? yes |
| 11 | +# Enter a dollar amount: 0.67 |
| 12 | +# 2 quarters, 1 dime, 1 nickel, 2 pennies |
| 13 | +# Would you like make more change? no |
| 14 | +# Have a good day! |
| 15 | + |
| 16 | +# Version 2 (optional) |
| 17 | + |
| 18 | +# Instead of hard-coding the coins, store them in a list of tuples. This way you can make custom coins. |
| 19 | + |
| 20 | +# coins = [ |
| 21 | +# ('half-dollar', 50), |
| 22 | +# ('quarter', 25), |
| 23 | +# ('dime', 10), |
| 24 | +# ('nickel', 5), |
| 25 | +# ('penny', 1) |
| 26 | +# ] |
| 27 | + |
| 28 | +#============================================================================================================================================================ |
| 29 | + |
| 30 | +more_change = "yes" |
| 31 | +while more_change == "yes": |
| 32 | + |
| 33 | + print("Welcome to the Change Maker 5000 (tm)") |
| 34 | + dollar_amount = float(input("Enter a dollar amount: ")) |
| 35 | + remaining_change = dollar_amount |
| 36 | + |
| 37 | + make_half_dollar = remaining_change // 0.50 |
| 38 | + make_quarter = remaining_change // 0.25 |
| 39 | + make_dime = remaining_change // 0.10 |
| 40 | + make_nickel = remaining_change // 0.05 |
| 41 | + make_penny = remaining_change // 0.01 |
| 42 | + |
| 43 | + |
| 44 | + change_amount = { |
| 45 | + 'half_dollar': 0, |
| 46 | + 'quarter': 0, |
| 47 | + 'dime': 0, |
| 48 | + 'nickel': 0, |
| 49 | + 'penny': 0 |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + while remaining_change > 0.00: |
| 54 | + # print(change_amount) |
| 55 | + # print(remaining_change) |
| 56 | + if remaining_change >= 0.50: |
| 57 | + remaining_change = remaining_change - 0.50 |
| 58 | + change_amount['half_dollar'] += 1 |
| 59 | + elif remaining_change >= 0.25: |
| 60 | + remaining_change = remaining_change - 0.25 |
| 61 | + change_amount['quarter'] += 1 |
| 62 | + elif remaining_change >= 0.10: |
| 63 | + remaining_change = remaining_change - 0.10 |
| 64 | + change_amount['dime'] += 1 |
| 65 | + elif remaining_change >= 0.05: |
| 66 | + remaining_change = remaining_change - 0.05 |
| 67 | + change_amount['nickel'] += 1 |
| 68 | + elif remaining_change >= 0.01: |
| 69 | + remaining_change = remaining_change - 0.01 |
| 70 | + change_amount['penny'] += 1 |
| 71 | + # print(remaining_change) |
| 72 | + |
| 73 | + remaining_change = round(remaining_change, 2) |
| 74 | + print(change_amount) |
| 75 | + |
| 76 | + more_change = input("Would you like make more change?: ") |
0 commit comments