16 lines
556 B
Python
16 lines
556 B
Python
|
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])
|