Python Forum

Full Version: Question about testing for equivlance
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
I am clearly confused on testing for equivlance.

Why does the below print yes even though roll is not set to a matching number?

roll = 12
if roll == 6 or 7 or 11 or 16 or 19:
    print ("yes")
    print (roll)

I see I need | but why does with the or keyword it come out as a match?
There is a great tutorial on the subject on our forums.
(May-08-2018, 07:51 PM)j.crater Wrote: [ -> ]There is a great tutorial on the subject on our forums.

Wow that was fantastic. Too bad he didnt cover integers cause I have some problems with those and if you try == it doenst like it

roll = 15
if roll == 6 or == 7 :
    print ("yes")
    print (roll)
Will try putting them in "" to follow the article.

Edit I see what i did wrong i didnt use value a 2nd time. I am going to try it the more pythonic way he suggests and see how that works out
In Python any number different from zero is evaluated to True.
In the if statement you are using 'or' so if the number isn't zero the whole expression is True.

To check if a number is presented in a list... Well, you speak English better than me Smile
The tutorial applies to both integers and strings. You need syntax like:
if value == "Yes" or value == "Y":

# or

if value in ("Yes", "Y", "yes", "y", "yay", "jup", "argh"):
Lewis