I want to get an input from user. If I want a string as an input, then I can have input("enter your answer")
But if I want the input as a boolean value: True or False ONLY, how can I do it in python?
-
1see thisYoav Ben Haim– Yoav Ben Haim2021-07-03 19:57:15 +00:00Commented Jul 3, 2021 at 19:57
Add a comment
|
1 Answer
You can either:
- Create a separate method that asks for input of strings like
"True"or"False"and that use that method as a boolean according to what it returns:
def trueCheck(): response = input("True or False?") if response == "True": return True if response == "False": return False
- Or pass the response from
_mainto the method and return a boolean:
def trueCheck(response): if response == "True": return True if response == "False": return False