TDT4109/common/inputChecking/boolInput.py

28 lines
892 B
Python

def boolInput(question, error='Skriv in J eller N\n', yesNoLetters=('j', 'n')):
"""
Asks the user a yes/no question and returns a bool based on the input.
Parameters: \\
prompt (str): The prompt asking the user for input \\
error? (str): The message to be printed on parsing error \\
yesNoLetters? ((str,str)): The letters to be used for representing yes and no in lower caps
"""
yesLetters = [yesNoLetters[0], yesNoLetters[0].capitalize()]
while True:
try:
choice = input(question)
assert validateInput(choice, yesNoLetters)
return choice in yesLetters
except:
print(error)
def validateInput(input, yesNoLetters):
yesLetters = [yesNoLetters[0], yesNoLetters[0].capitalize()]
noLetters = [yesNoLetters[1], yesNoLetters[1].capitalize()]
try:
assert input in yesLetters + noLetters
return True
except:
return False