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 yesNoLetters? ((str,str)): The letters to be used for representing yes and no in lower caps
""" """
yesLetters = [yesNoLetters[0], yesNoLetters[0].capitalize()] yesLetters = [yesNoLetters[0], yesNoLetters[0].capitalize()]
noLetters = [yesNoLetters[1], yesNoLetters[1].capitalize()]
while True: while True:
try: try:
choice = input(question) choice = input(question)
assert choice in yesLetters + noLetters assert validateInput(choice, yesNoLetters)
return choice in yesLetters return choice in yesLetters
except AssertionError: except:
print(error) 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: while True:
inputValue = input(prompt) inputValue = input(prompt)
try: try:
assert validateInput(inputValue, type)
return type(inputValue) return type(inputValue)
except ValueError: except AssertionError:
print(error) print(error)
def validateInput(input, type):
try:
_ = type(input)
return True
except:
return False