2020-09-15 11:49:55 +02:00
|
|
|
def inputTypeCheck(
|
|
|
|
prompt,
|
|
|
|
type,
|
|
|
|
error="Kunne ikke tolke input. Har du skrevet det inn riktig?"
|
|
|
|
):
|
|
|
|
"""
|
|
|
|
Typechecks an input, and only returns the input when it could be successfully parsed.
|
|
|
|
|
|
|
|
Parameters: \\
|
|
|
|
prompt (str): The prompt asking the user for input \\
|
|
|
|
type (fun): The function to be used for parsing \\
|
|
|
|
error? (str): The message to be printed on parsing error
|
|
|
|
"""
|
|
|
|
while True:
|
|
|
|
inputValue = input(prompt)
|
|
|
|
try:
|
2020-10-07 13:13:12 +02:00
|
|
|
assert validateInput(inputValue, type)
|
2020-09-15 11:49:55 +02:00
|
|
|
return type(inputValue)
|
2020-10-07 13:13:12 +02:00
|
|
|
except AssertionError:
|
|
|
|
print(error)
|
|
|
|
|
|
|
|
def validateInput(input, type):
|
|
|
|
try:
|
|
|
|
_ = type(input)
|
|
|
|
return True
|
|
|
|
except:
|
|
|
|
return False
|