TDT4109/common/inputChecking/choiceInput.py

18 lines
593 B
Python

def choiceInput(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 lower caps)
Returns the choice in lower 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])