Split input functions for custom implementations

This commit is contained in:
Oystein Kristoffer Tveit 2020-10-07 13:13:12 +02:00
parent d9c5d7d400
commit 72478e8199
2 changed files with 24 additions and 6 deletions

View File

@ -8,11 +8,21 @@ def boolInput(question, error='Skriv in J eller N\n', yesNoLetters=('j', 'n')):
yesNoLetters? ((str,str)): The letters to be used for representing yes and no in lower caps
"""
yesLetters = [yesNoLetters[0], yesNoLetters[0].capitalize()]
noLetters = [yesNoLetters[1], yesNoLetters[1].capitalize()]
while True:
try:
choice = input(question)
assert choice in yesLetters + noLetters
assert validateInput(choice, yesNoLetters)
return choice in yesLetters
except AssertionError:
print(error)
except:
print(error)
def validateInput(input, yesNoLetters):
yesLetters = [yesNoLetters[0], yesNoLetters[0].capitalize()]
noLetters = [yesNoLetters[1], yesNoLetters[1].capitalize()]
try:
assert input in yesLetters + noLetters
return True
except:
return False

View File

@ -14,6 +14,14 @@ def inputTypeCheck(
while True:
inputValue = input(prompt)
try:
assert validateInput(inputValue, type)
return type(inputValue)
except ValueError:
print(error)
except AssertionError:
print(error)
def validateInput(input, type):
try:
_ = type(input)
return True
except:
return False