TDT4109/common/inputChecking/choiceInput.py

18 lines
593 B
Python
Raw Normal View History

2020-09-16 15:01:12 +02:00
def choiceInput(prompt, choices):
2020-09-15 11:49:55 +02:00
"""
Prompts the user to make a choice and asserts that the choice is valid.
Parameters: \\
prompt (str): The prompt asking the user for input \\
2020-09-16 15:01:12 +02:00
choices ([str]): The choices that the user can choose (in lower caps)
Returns the choice in lower caps
2020-09-15 11:49:55 +02:00
"""
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])