1

Code works from count=0 to count=9. Then it does not get in to the other elif code. I have commented which part is not working below. I have tried so many time by checking the value of count, still I could not find why the code doesn't work

count =0
for line in sys.stdin:
  line = line.strip()
  print(count)
  if (count==0):
    a = int(line)                   #no of series
    count=1


  elif(count==1):             #2nd line 2 players 3 mtches
    plyrs_mtchs=[]  
    plyrs_mtchs= line.split()
    print(plyrs_mtchs)
    count+=1                        # #no of players , no of matches

  elif(count==2 or count==6):              
    players.append(str(line))
    print(players)
    count+=1


  elif(count==3,5 or count==7,9):               
    currenplyr = players[len(players)-1]        
    predict.append(line.split())
    count+=1


  elif(count==10 or count==11): #this code doesn't work      
    actual.append(line.split())
    count+=1

  elif(count==12):              #this code doesnt work
    actual.append(line.split())
    count+=1
3
  • elif(count==3,5 or count==7,9) won't work either. Commented Oct 24, 2015 at 14:37
  • It works well no problem with it Commented Oct 24, 2015 at 14:41
  • On the contrary, that is exactly your problem. Commented Oct 24, 2015 at 14:41

1 Answer 1

5

Your expression:

elif(count==3,5 or count==7,9):               

does not do what you think it does. You are not testing if count has one of 4 possible values there. Python sees this as 3 different expressions forming a tuple:

count==3
5 or count==7
9

producing the output

(False, 5, 9)

if count is not equal to 3 and

(True, 5, 9)

if it is. 5 or <some other expression will always return 5 because it is a non-zero numeric value, so true, and it doesn't matter what the other side of the or operator evaluates as anymore.

A tuple with 3 elements is always true in a boolean context, because it is a non-empty container. As such, that elif branch is always going to match if preceding if or elif tests failed.

See the Truth Value Testing and Boolean Operations sections for details on how boolean testing and or work.

Use the in operator membership test instead:

elif count in {3, 5, 7, 9}:

where the test will be true if count has a value that's in the set of 4 possible values.

Sign up to request clarification or add additional context in comments.

4 Comments

@Pieters: Very nice and detailed answer. Just curious why you prefer a set in count in {1, 2} over a much simpler constant object - a tuple - like count in (1, 2)? Has the set an advantage? Thanks.
@VPfB yes, set membership tests are faster (O(1) constant time). The Python 3 compiler actually stores a frozenset() object with the bytecode as it cannot be mutated anyway.
@Pieters: Just tried a quick timing test and the set is faster indeed. Did not expect that for a really small set, because a hash must be computed. Learned something new again. Thanks.
finally I found what it really was. Thanks for the explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.