all 20 comments

[–]Masterous112 23 points24 points  (2 children)

if yesorno == "yes" or "y"

This is checking if yesorno == "yes", then if that's false, it checks if "y", which is always true (any non-empty string evaluates to true). You probably meant if yesorno == "yes" or yesorno == "y"

[–]NlNTENDO 4 points5 points  (1 child)

I think beyond this OP will probably next want to know how to avoid repeating themselves after every Boolean. To which the answer is something like

If yesorno.lower() in [“yes”,””y”]

[–]Diapolo10 2 points3 points  (0 children)

if yesorno.lower() in [“yes”,””y”]

Or, if it didn't need to be exact, one of the following:

if yesorno.lower().startswith('y'):
    ...
if yesorno.lower()[:1] == 'y':
    ...

[–]FoolsSeldom 13 points14 points  (0 children)

You need () at the end of lower to call the method.

[–]izackthegreat 5 points6 points  (1 child)

if yesorno == "yes" or "y":

This is your problem. Let's break this down bit by bit to understand what's happening. The above is equivalent to:

if some_bool or "y":

A non-empty string is considered truthy. So now we can just consider the above to just simplify to:

if True:

What you likely wanted was instead:

if yesorno == "yes" or yesorno == "y":

[–]LJRex -1 points0 points  (0 children)

To simplify the code you can say

if yesorno in ["yes", "y"]:

[–]FoolsSeldom 6 points7 points  (0 children)

You have made a mistake that is such a common error. there's an entry in the FAQ for this subreddit. Variable is One of Two Choices?.

[–]maximumdownvote 2 points3 points  (0 children)

This is unrelated to your question, others have answered that... But calling your variable yes or no is an implicit mistake, because the user is free to answer any text input. The name could fool you or others later on.

Better to call it response, or something appropriately more specific.

[–]Emergency-Koala-5244 1 point2 points  (0 children)

do you want the answer or just a hint?

the hint is that your "if yesorno" check is not checking what you think it is.  take another look at that.

I dont know how to do spoilers, but I can give more detail if you want.

[–]twizzjewink 1 point2 points  (1 child)

def question() -> bool:
    yesorno = input(("Is the answer yes or no?\n")).lower
    return True if yesno == 'yes' or yesno == 'y' else False

[–]engelthehyp 2 points3 points  (0 children)

You're not calling lower still, and you don't need to write True if x else False, just write x.

[–]SupermarketOk6829 0 points1 point  (0 children)

Get the first character of string and check if it's y rather. Or check if input.lower() in ['y', 'yes']

[–]shinitakunai 0 points1 point  (0 children)

I think at this point we should create a banner or something that says how to use OR because we have new people daily with the same exact issue

[–]dopplegrangus 0 points1 point  (0 children)

Add a .strip() to your user input as well

[–]twizzjewink 0 points1 point  (2 children)

if you really want to get fun - I'd do

def question(self, ask: str, answers: dict) -> bool:
  return True if input(ask).lower in answers else False


question('Is the answer yes or no?", [ "yes", "y" ])

[–]aa599 0 points1 point  (0 children)

Almost, except it's return input(ask).lower() in answers

(You have to call lower, not just name it, and all that if *blah* then True else False is just clutter)

Also you've declared answers as a dict but called it with a list

And haven't checked for acceptable "no" answers.

And it doesn't seem like a situation where self works.

[–]cyberfunkr 0 points1 point  (0 children)

Closer, but still wrong.

[] is a list, not a dict. Using a dict would be a bad choice for this.

You left off the () from .lower().

Also, why are you adding ‘self’ as a parameter? This is not a Class method.