dimanche 17 octobre 2021

Why can't I check multiple values against a single variable but instead have to check the variable against each value individually? [duplicate]

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.

Aucun commentaire:

Enregistrer un commentaire