0
import random

varA = random.randint(0,20)

print(varA)

if (varA == 0 or 1 or 2 or 3 or 4 or 5 or 6 or 7 or 8):
    print("set 1")

elif(varA == 9 or 10 or 11 or 12 or 13 or 14 or 15):
    print("set 2")

elif(varA == 16 or 17 or 18 or 19 or 20):
    print("set 3")

So no matter what number varA becomes, it prints "set 1." I know it's probably something ridiculously obvious but I'm extremely new to Python and programming overall, I was just messing around with elif and can't figure what's wrong here. Thanks!

BrenBarn
  • 242,874
  • 37
  • 412
  • 384
Scope
  • 11
  • 2

4 Answers4

4

Or doesn't really work like the English or. You should do it as this instead:

if (varA in range(9)):
    print("set 1")

elif(varA in range(9,16)):
    print("set 2")

elif(varA in range(16,21)):
    print("set 3")

Alternatively, you could do:

if (0 =< varA <= 8):
    print("set 1")

elif (9 =< varA <= 15):
    print("set 2")

elif (16 =< varA <= 20):
    print("set 3")
sshashank124
  • 31,495
  • 9
  • 67
  • 76
  • While the part about `or` not working like English is true, `in range(anything)` is a horrible habit to get into. – user2357112 Apr 09 '14 at 08:13
  • @user2357112. I have suggested an alternate method. Thank you – sshashank124 Apr 09 '14 at 08:14
  • Again, you can also do `if (0 <= varA <= 8):` – Torxed Apr 09 '14 at 08:14
  • Thanks for the answers (that goes out to everyone), I had organized them without "or" earlier but was creating the range incorrectly I guess so I figured I'd try to be as thorough as possible which is when I started using "or" In any case, thanks again everyone! – Scope Apr 09 '14 at 08:36
  • @user3514234, if my answer was helpful, would you mind accepting it? Thank you. – sshashank124 Apr 09 '14 at 08:38
  • I had to think a minute about the Problem with `in range()`, resulting in a comparison of (worst case) each element. If python only had a type system and a properly overloaded `in` operator for ranges... – ted Apr 09 '14 at 08:49
  • 1
    @ted: Python *does* have a type system. What are you talking about? It's just that in the 2.x line, `range` returns a list for historical reasons. `xrange` is bare-bones, so it doesn't support efficient `in` for simplicity, but Python 3 `range` objects have an efficient `in` operator. – user2357112 Apr 09 '14 at 11:05
  • @user2357112 While I did not look that much into the details of `in`, that is sort of the point. I fail to see the issue with a properly overloaded `__contains__`. – ted Apr 09 '14 at 13:37
1
if (varA == 0 or varA == 1 ...):
    print("set 1")

You need to compare 1 against varA again, you can't do VarA == 0 or 1 because that will theoreticly check if 1 is True, which it is.

So i could translate your code into:

if (varA == 0 or 1 == True or 2 == True ...)

Any number of 1 or more will become True.
This is the reason why your code fails, @sshashank beat me to the range() solution so i'll be satisfied with simply explaining where you went wrong.

Torxed
  • 22,866
  • 14
  • 82
  • 131
  • Why the downvote? This answers the OP's question and further more explains why it fails.. which no one else explained.... – Torxed Apr 09 '14 at 08:12
1
if varA in [0, 1, 2, 3, 4, 5, 6, 7, 8]:

Better:

if varA >= 0 and varA < 8:

Even better:

if 0 <= varA < 8:
0

The or statements in the if statement will always make the expression true. This is because you are doing a logical or on values 1, 2, 3... which will always be true.

wyas
  • 377
  • 2
  • 14