-1

can someone explain me why when I use this if statement:

if template_decider != ("B" or "P"):
    something_went_wrong()
else:
    print("Hello")

If user inputs B the statement returns "Hello" but if user press P it resturns function something_went_wrong?

I'm really confused why it not always returns "Hello"

Thank you.

battle44
  • 55
  • 9
  • 8
    Does this answer your question? [How to test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) In other words, `template_decider != ("B" or "P")` should be `template_decider not in ["B", "P"]` – Random Davis Dec 09 '20 at 17:04
  • `template_decider != ("B" or "P")` is not the right way to test for multiple values. – John Gordon Dec 09 '20 at 17:06
  • 1
    Try `print(("B" or "P"))`. In the python world, its "B" because `or` short circuits on the first True value and returns that value. – tdelaney Dec 09 '20 at 17:10

1 Answers1

0

You wrote incorrect statement. But it's possible to do what you want with this code:

if template_decider not in ("B", "P"):
    something_went_wrong()
else:
    print("Hello")
Vlad Havriuk
  • 1,291
  • 17
  • 29
  • I don't think this will work as intended. Looks like OP wants it to only print "Hello" if `p` is pressed – lime Dec 09 '20 at 17:11
  • @lime for me, it seems more like checking if `template_decider` is a valid answer, which should be either `B` or `P` – Vlad Havriuk Dec 09 '20 at 17:14
  • based on the way OP code is written the code I agree, but it's specifically stated in the question that `If user inputs B the statement returns "Hello" ` – lime Dec 09 '20 at 17:16
  • @lime oh right.. I payed attention at code first ._. – Vlad Havriuk Dec 09 '20 at 17:25
  • 1
    It's okay, I almost did the same thing haha! Easy to get lost in the code sometimes – lime Dec 09 '20 at 17:37