lundi 29 juillet 2019

Exit If Statement if returned value is not a boolean

I have a user input for a yes or no answer. I would like to construct a function in python 3 which I could pass the value like isTrue('yes) and then the function would return True and isTrue('no') would return False. I am aware that I could just use an if statement to see if the value is just yes or no, but for other reasons, I'd benefit from using a function like isTrue(). Creating the function isTrue() is very easy, but the part I am struggling with is how to handle a value that is not true, nor false, like maybe or IDK. Ideally, I'd like to have an if statement like so

if isTrue(userInput) '''Some code here that would run anther function if the value was not true or false''':
    print('Input was Ture')
else:
    print('Input was False')

Is there a way I could return a value that I could still have the same if statement structure, but detect if the value wasn't True or False without having to do something like this and somehow have it integrated with the True if statement?

if isTrue(userInput):
    print('Input was Ture')
else !isTrue(userInput):
    print('Input was False')
else:
    print('Invalid')

Aucun commentaire:

Enregistrer un commentaire