19 lines
529 B
Python
19 lines
529 B
Python
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:
|
|
return type(inputValue)
|
|
except ValueError:
|
|
print(error) |