From 72478e81994ca6b12bcf53a317e53bd70fe3dd17 Mon Sep 17 00:00:00 2001 From: h7x4 Date: Wed, 7 Oct 2020 13:13:12 +0200 Subject: [PATCH] Split input functions for custom implementations --- common/inputChecking/boolInput.py | 18 ++++++++++++++---- common/inputChecking/typeCheck.py | 12 ++++++++++-- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/common/inputChecking/boolInput.py b/common/inputChecking/boolInput.py index 0f18294..fd2e586 100644 --- a/common/inputChecking/boolInput.py +++ b/common/inputChecking/boolInput.py @@ -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) \ No newline at end of file + 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 \ No newline at end of file diff --git a/common/inputChecking/typeCheck.py b/common/inputChecking/typeCheck.py index 534e9dd..a8a349b 100644 --- a/common/inputChecking/typeCheck.py +++ b/common/inputChecking/typeCheck.py @@ -14,6 +14,14 @@ def inputTypeCheck( while True: inputValue = input(prompt) try: + assert validateInput(inputValue, type) return type(inputValue) - except ValueError: - print(error) \ No newline at end of file + except AssertionError: + print(error) + +def validateInput(input, type): + try: + _ = type(input) + return True + except: + return False \ No newline at end of file