1

I want to ask the user a true/false question and get a boolean value from it, but I don't really understand how .bool() works. This is more of a "what can I do?' than a "what's the problem?".

So for example, this is what my code is looking like:

question = input("Is the Earth flat? ")
if question.bool() is True:
     print("You dumb.")

I know this isn't correct but I'm not sure what would work!

What makes the .bool() register True or False?

I thought this might be what I have to do but it doesn't seem right:

if question == "Yes" or "Yeah" or "Y" or "Duh".lower()
     print("You dumb.")

6 Answers6

2

Use in for this, not multiple or statements as it’s less readable in its correct form and less efficient. Also sets are great for checking inclusivity.

question = input("Is the Earth flat? ")
if question.lower() in {"yes", "yeah", "y", "duh"}:
     print("You dumb.")

You could even make a custom function to ask a question and return a Boolean result

def ask_bool(question):
    return input(question).lower() in {"yes", "yeah", "y", "duh"}

Then to ask for a Boolean answer:

if ask_bool("Is the Earth flat? "):
     print("You dumb.")
Jab
  • 26,853
  • 21
  • 75
  • 114
1

Use element in list:

if question.lower() in ["yes", "yeah", "y", "duh"]:
     print("You dumb.")
DjaouadNM
  • 22,013
  • 4
  • 33
  • 55
0

For your second option, this does not work because python doesn't understand what the other conditions are. The correct way to check multiple conditions for equality is the following

if (question == "Yes" or question == "Yeah" or question == "Y" or question == "Duh".lower())
     print("You dumb.")

However, this is probably not the best way to do this with this many terms, and you're better off storing them in a list and iterating through.

Mike
  • 1,471
  • 12
  • 17
0

I would reccommend to use elements in list: between the [] you just have to list all the possible answers that you can accept and process.

if question.lower() in ["Yes", "YES", "true", "TRUE"]:
     print("You dumb!")
else:
     print("You are not stupid!")

Hope this helped.

0

I got a solution with default yes answer

ans = input("Your answer [Y/n]: ")

if ans.lower() == "n":
    # nooooo 
else:
    # yessss 

However you should see this too, https://stackoverflow.com/a/48817835/10362396

tbhaxor
  • 1,659
  • 2
  • 13
  • 43
0
question = input("Is the Earth flat? ")

if question.lower() == "Yes".lower() or question.lower() == "Yeah".lower():
  print(question)
  print("You dumb.")
Briny
  • 11
  • 3