shanenin Posted June 24, 2005 Report Share Posted June 24, 2005 (edited) I feel like I am going crazy. I do not see why this script is behaving the way it is. def winner(): pool = [1,2,5,6] winner = ( (1,2,3), (4,5,6), (7,8,9), (1,4,7), (2,5,8), (3,6,9), (1,5,9), (3,5,7) ) for i in winner: if (i[0] and i[1] and i[2]) in pool: print iwinner()here is what I think this script should be doing. it should check every element in the tuple called winner; if all three numbers that comprise the element are in the list called pool, it will print the element from the tuple winner. here is where it is seeming to go wrong. it is printing the element (4,5,6). The numbers 4,5, and 6 are not in the list pool. How could this be possible.edit added later//my if statement is saying: if the numbers 4,5, and 6 are in the list pool then print 4,5,6the numbers are not in the list pool, so how could it be printing them? Edited June 24, 2005 by shanenin Quote Link to post Share on other sites
shanenin Posted June 24, 2005 Author Report Share Posted June 24, 2005 I needed to do the line like thisif i[0] in pool and i[1] in pool and i[2] in pool:the way I was doing it was like sayingif 5 in pool: #i[2] = 5 it seems to only except the last elemnt in the argument Quote Link to post Share on other sites
jcl Posted June 24, 2005 Report Share Posted June 24, 2005 (edited) The description of the 'and' operator reads"The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned."if i[0] and i[1] and i[2] in pool:is equivalent to if ((i[0] and i[1]) and i[2]) in pool:every element of i is true, so this reduces like soif ((i[0] and i[1]) and i[2]) in pool:--> if (i[1] and i[2]) in pool:--> if i[2] in pool: Edited June 24, 2005 by jcl Quote Link to post Share on other sites
shanenin Posted June 24, 2005 Author Report Share Posted June 24, 2005 thanks, that makes sence :-) Quote Link to post Share on other sites
jcl Posted June 24, 2005 Report Share Posted June 24, 2005 (edited) You're far from the only person who's wanted that sort of operator chaining to work. There are a geat many people who really wanta < b < cto mean(a < b) and (b < c)especially in numerical or otherwise math-oriented code, but there are very few languages in which it does. The only one that comes to mind is Perl 6. (The Lisp family supports something equivalent, but not with that syntax.)It's a bit of a sensitive subject in programming language design.[Edit: Really bad typo. Though 'operator changing' is accurate, since the meaning of the operators would change depending on the surround context. 'a < b' alone would produce in boolean result but 'a < b' in 'a < b < c' would produce... something odd. The oddness being part of the reason chaining isn't support.] Edited June 24, 2005 by jcl Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.