TDT4109/common/inputChecking/typeCheck.py
2020-09-15 11:49:55 +02:00

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)