Python Forum
Question about testing for equivlance
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question about testing for equivlance
#1
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?
Reply
#2
There is a great tutorial on the subject on our forums.
Reply
#3
(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
Reply
#4
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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
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
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020