Split input functions for custom implementations
This commit is contained in:
parent
d9c5d7d400
commit
72478e8199
|
@ -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
|
|
@ -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
|
Loading…
Reference in New Issue