Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Coin Change Exercise Program
- import random
- #greeting and instructions
- print('The purpose of this exercise is to enter a number of coin values')
- print('that add up to a displayed target value. \n')
- print('Enter coins values as 1-penny, 5-nickel, 10-dime and 25-quarter')
- print('Hit return after the last entered coin value.')
- print('------------------')
- #initializing variables
- terminate = False
- empty_str = ''
- #start game sequence
- while not terminate:
- amount = random.randint(1,99)
- print('Enter coins that at up to', amount, 'cents, one per line.\n')
- game_over = False
- total = 0
- while not game_over:
- valid_entry = False
- while not valid_entry:
- if total == 0:
- entry = input('Enter first coin: ')
- else:
- entry = input('Enter next coin: ')
- if entry in (empty_str, '1', '5', '10', '25'):
- valid_entry = True
- else:
- print('Invalid Entry')
- if entry == empty_str:
- if total == amount:
- print('Correct!')
- else:
- print('Sorry - you only entered', total, 'cents.')
- game_over = True
- else:
- total = total + int(entry)
- if total > amount:
- print('Sorry - total amount exceeds', amount, 'cents.')
- game_over = True
- if game_over:
- entry = input('\n Try again (y/n)?: ')
- if entry == 'n':
- terminate = True
- print('Thank you for playing... goodbye')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement