Python Forum
Question about testing for equivlance - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Question about testing for equivlance (/thread-10007.html)



Question about testing for equivlance - fad3r - May-08-2018

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?


RE: Question about testing for equivlance - j.crater - May-08-2018

There is a great tutorial on the subject on our forums.


RE: Question about testing for equivlance - fad3r - May-08-2018

(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


RE: Question about testing for equivlance - wavic - May-08-2018

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


RE: Question about testing for equivlance - ljmetzger - May-08-2018

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