1

I'm sure there is a way to simplify this if statement but I don't remember it and after googling for a while I couldn't find it. Any ideas?

if user_input.lower() == 'yes' or user_input.lower() == 'y':
    print('Done!')
1
  • 1
    Using a tuple with the in operator will give you a slightly better performance than a list or a set. Commented Nov 8, 2022 at 18:14

1 Answer 1

2

You can check whether a value is present in an iterable using the in keyword: user_input.lower() in ("yes", "y") will evaluate to True if the input is either 'yes' or 'y'.

Alternatively, for more complex checks, you might consider any(), which takes an iterable of boolean statements and returns True if any of them are True. For example, the statement any([user_input.lower() in ("yes", "y"), skip_check, ignore_input]), where skip_check and ignore_input are some other boolean flags you might care about, will be True if any of those things are true.

all() is similar to any() but, as the name suggests, requires that all elements be true in order to come out True. You can think of any() as being analogous to a chained or statement, while all() would be like replacing those ors with and.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.