Randomly display an amount of change and ask how many of each coin type are needed to equal the amount displayed.
(self)
| 19 | print('*' * 75) |
| 20 | |
| 21 | def start(self): |
| 22 | """Randomly display an amount of change and ask how many of each coin |
| 23 | type are needed to equal the amount displayed. |
| 24 | """ |
| 25 | self.display_intro() |
| 26 | currency_amt = random.randint(1, 99) |
| 27 | print('\nHow much change is needed to equal .{0} cents?\n' |
| 28 | .format(str(currency_amt))) |
| 29 | your_total_amt = get_input_values(currency_amt) |
| 30 | |
| 31 | if sum(your_total_amt) == 0: |
| 32 | print('Thank you for playing.') |
| 33 | sys.exit(0) |
| 34 | else: |
| 35 | if your_total_amt[0] > 1 or your_total_amt[0] == 0: |
| 36 | quarter_spelling = 'quarters' |
| 37 | else: |
| 38 | quarter_spelling = 'quarter' |
| 39 | |
| 40 | if your_total_amt[1] > 1 or your_total_amt[1] == 0: |
| 41 | dime_spelling = 'dimes' |
| 42 | else: |
| 43 | dime_spelling = 'dime' |
| 44 | |
| 45 | if your_total_amt[2] > 1 or your_total_amt[2] == 0: |
| 46 | nickel_spelling = 'nickels' |
| 47 | else: |
| 48 | nickel_spelling = 'nickel' |
| 49 | |
| 50 | if your_total_amt[3] > 1 or your_total_amt[3] == 0: |
| 51 | penny_spelling = 'pennies' |
| 52 | else: |
| 53 | penny_spelling = 'penny' |
| 54 | |
| 55 | print('\nCorrect! You entered {0:d} {1}, {2:d} {3},' |
| 56 | ' {4:d} {5} and {6:d} {7}.'.format(your_total_amt[0], |
| 57 | quarter_spelling, |
| 58 | your_total_amt[1], |
| 59 | dime_spelling, |
| 60 | your_total_amt[2], |
| 61 | nickel_spelling, |
| 62 | your_total_amt[3], |
| 63 | penny_spelling)) |
| 64 | print('Which equals .{0} cents. Nice job!' |
| 65 | .format(str(currency_amt))) |
| 66 | |
| 67 | response = input('\nWould you like to try again? ') |
| 68 | if response.lower() != 'y': |
| 69 | print('Thanks for playing.') |
| 70 | sys.exit(0) |
| 71 | self.start() |
| 72 | |
| 73 | |
| 74 | def get_input_values(currency_amt): |
no test coverage detected