TDT4109/common/inputChecking/choiceInput.py

16 lines
556 B
Python
Raw Normal View History

2020-09-15 11:49:55 +02:00
def safeQuestion(prompt, choices):
"""
Prompts the user to make a choice and asserts that the choice is valid.
Parameters: \\
prompt (str): The prompt asking the user for input \\
choices ([str]): The choices that the user can choose (in low caps)
"""
allChoices = choices + [choice.capitalize() for choice in choices]
while True:
try:
answer = input(prompt)
assert answer in allChoices
return answer.lower()
except AssertionError:
print('Skriv inn enten', ', '.join(choices[:-1]), 'eller', choices[-1])