Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. # Coin Change Exercise Program
  2.  
  3. import random
  4.  
  5. #greeting and instructions
  6. print('The purpose of this exercise is to enter a number of coin values')
  7. print('that add up to a displayed target value. \n')
  8. print('Enter coins values as 1-penny, 5-nickel, 10-dime and 25-quarter')
  9. print('Hit return after the last entered coin value.')
  10. print('------------------')
  11.  
  12. #initializing variables
  13. terminate = False
  14. empty_str = ''
  15.  
  16. #start game sequence
  17. while not terminate:
  18. amount = random.randint(1,99)
  19. print('Enter coins that at up to', amount, 'cents, one per line.\n')
  20. game_over = False
  21. total = 0
  22.  
  23. while not game_over:
  24. valid_entry = False
  25.  
  26. while not valid_entry:
  27. if total == 0:
  28. entry = input('Enter first coin: ')
  29. else:
  30. entry = input('Enter next coin: ')
  31.  
  32. if entry in (empty_str, '1', '5', '10', '25'):
  33. valid_entry = True
  34. else:
  35. print('Invalid Entry')
  36. if entry == empty_str:
  37. if total == amount:
  38. print('Correct!')
  39. else:
  40. print('Sorry - you only entered', total, 'cents.')
  41.  
  42. game_over = True
  43. else:
  44. total = total + int(entry)
  45. if total > amount:
  46. print('Sorry - total amount exceeds', amount, 'cents.')
  47. game_over = True
  48.  
  49. if game_over:
  50. entry = input('\n Try again (y/n)?: ')
  51.  
  52. if entry == 'n':
  53. terminate = True
  54. print('Thank you for playing... goodbye')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement