-2

I have started learning python on my own for fun and I was writing this and did not get the results i wanted:

if no1234 == 0:
     print "Player and Computer tie!\n"
elif no1234 == 1 or 2:
     print "Player wins!\n"
elif no1234 == 3 or 4:
     print "Computer wins!\n"

computer wins would not show up, is there something im forgetting

Ashwini Chaudhary
  • 232,417
  • 55
  • 437
  • 487

2 Answers2

5

elif no1234 == 1 or 2: is parsed as elif (no1234 == 1) or (2):

It will always be True because bool(2) == True and you're using an or statement.

You probably wanted:

elif no1234 == 1 or no1234 == 2:

Or even:

elif no1234 in (1, 2):

This is the same for your other elif.


So altogether:

if no1234 == 0:
     print "Player and Computer tie!\n"
elif no1234 in (1, 2):
     print "Player wins!\n"
elif no1234 in (3, 4):
     print "Computer wins!\n"
TerryA
  • 56,204
  • 11
  • 116
  • 135
3

You need this:

if no1234 == 0:
     print "Player and Computer tie!\n"
elif no1234 == 1 or no1234 == 2:
     print "Player wins!\n"
elif no1234 == 3 or no1234 == 4:
     print "Computer wins!\n"
Salvador Dali
  • 199,541
  • 138
  • 677
  • 738