-1

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
  • 244,495
  • 58
  • 464
  • 504

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
  • 58,805
  • 11
  • 114
  • 143
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
  • 214,103
  • 147
  • 703
  • 753