On Tue, Nov 17, 2009 at 12:02 AM, Mr.SpOOn <span dir="ltr"><<a href="mailto:mr.spoon21@gmail.com">mr.spoon21@gmail.com</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi,<br>
I'm trying to use logical operators (or, and) with the "in" statement,<br>
but I'm having some problems to understand their behavior.<br></blockquote><div><br>Hey Carlo, I think your issue here is mistaking 'in' as a statement. It's just another logic operator, much like 'and' and 'or'... it's not a statement in itself. At least, I don't think that's how you'd call 'in'.<br>
<br>You have to remember that Python's logical operators (and, or) works slightly different than most languages. It doesn't return True or False, if the things compared aren't boolean values. Instead, it returns the first thing that makes the decision.<br>
<br>For example a statement such as<br><br>>>>'3' and '4'<br>'4'<br><br>returns the string literal 4.<br><br>1.<br>Python's AND operator returns the first element if the first one is False; else, it returns the second element. That's all it does.<br>
<br>Python's OR operator returns the first element if the first one is True; else, it returns the second element.<br><br>Think about it. It's a little strange, but they don't return a pure Boolean value.<br><br>
2.<br>Only the empty string is considered False. Any non-empty strings have True values. <br><br>3.<br>The proper way of using the in operator is probably such: (There may be a better way I'm not aware of.)<br><br>>>> '3' in l and '4' in l<br>
False<br><br>>>> '3' in l and 'no3' in l<br>True<br><br>AND operator has a higher precedence, so you don't need any brackets here, I think. But anyway, you have to use it like that. So that's something you'll have to fix first.<br>
</div><div><br> </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
What I really need is to create a sequence of "if" statements to check<br>
the presence of elements in a list, because some of them are mutually<br>
exclusive, so if for example there are both "3" and "no3" it should<br>
return an error</blockquote><div><br>One idea I can think of is to have a presence count; assuming the input is non-repeating, you increase this count by 1, starting at 0. If you get anything above one, return this error you speak of, and that might be a good start.<br>
<br>Good luck,<br>Xav<br></div></div><br>