Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # simple function to prompt user for a yes or no response
- # and return True or False accordingly
- # pre-define sets of acceptable responses for yes or no
- AFFIRMATION = frozenset(('yes','y', 'yeh', 'yup', 'ok'))
- REJECTION = frozenset(('no', 'nah', 'nope', 'ney'))
- def yes_no(prompt="Yes or No? "):
- 'return True/False for affirmative/rejection response'
- 'do not accept an empty response or a response not in pre-defined sets'
- while True:
- response = input(prompt).lower()
- if response and response in AFFIRMATION:
- return True
- if response and response in REJECTION:
- return False
- print('Please respond either Yes or No.')
- # example usage
- while True: # main loop
- # do stuff
- if not yes_no('Play again? '):
- break
- print('bye')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement