0

I am learning python and trying to run some simple checks for a functions:

like for example,

x = input("Type a fruit's name: ")
if x == ("apple" or "Orange" or "Mango"):
    print("that's a fruit")
else: 
    print("Not a fruit")

But the output is True only for 'apple' and False for everything else.

I checked to see if code below works

x = input("Type a fruit's name: ")
if x == "apple" or x == "Orange" or x == "Mango":
    print("that's a fruit")
else: 
    print("Not a fruit")

and it does. Why does one works and the other only checks for the first value?

Just Curious.

Avinash
  • 11
  • 4
  • 2
    `x == ("apple" or "Orange" or "Mango"):` python isn't english, the `==` doesn't distribute over the logical connectives. This is exactly equzl to `x == "apple"` since `("apple" or "Orange" or "Mango")` evaluates to `"apple"`. – juanpa.arrivillaga Oct 18 '21 at 05:20
  • Does it help?: https://stackoverflow.com/a/45449148/6907424 – hafiz031 Oct 18 '21 at 05:24
  • @juanpa.arrivillaga Is there a better way to resolve this than checking for each value? because it would just become cumbersome for a large number of values. – Avinash Oct 18 '21 at 05:33
  • 2
    @AvinashSharma You can use `if x in ["apple", "Orange", "Mango"]:` – Selcuk Oct 18 '21 at 05:33
  • @Selcuk Thank You, I am dumb. I didn't even think to use a list. – Avinash Oct 18 '21 at 05:37

0 Answers0