My experience of confusing boolean expressions was the code:<br><br>>>> 1 == 2 in [2, False]<br>False<br><br>which, when parenthesised the two possible ways<br>>>> (1 == 2) in [2, False]<br>True<br>>>> 1 == (2 in [2, False])<br>
True<br>results in sensible values (although the second one's sensibility is debatable)<br><br>I couldn't figure it out quickly either. In fact, I ended up decompiling to byte code to figure out what was going on.<br>
<br>As far as other 'gotcha's in addition to the ones already mentioned:<br><br>Calling a file the same name as a module in the standard library. Import <name> will get the local file, not the stdlib one, which is confusing if you wanted the stdlib one.<br>
<br>I've noticed that students will somtimes put import statements inside a function - right before they need to use whatever they imported:<br>def area(r):<br> import math<br> return math.pi*r*r<br><br>Cheers,<br>Carl.<br>
<br><div class="gmail_quote">On 29 March 2011 18:06, Michael H. Goldwasser <span dir="ltr"><<a href="mailto:goldwamh@slu.edu">goldwamh@slu.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<br>
To start a new thread, I'm always trying to keep a list of some common<br>
"gotchas" that beginning students run across when using Python, or<br>
things that teachers should keep in mind when teaching with the<br>
language. I have in mind commands that do not generate runtime errors,<br>
but are likely to lead to logical errors that are not apparent to<br>
students, and most of these are legal due to the very flexible nature<br>
of the Python language. Some classicss are<br>
<br>
data.sort # no-op<br>
<br>
data = data.sort() # bye-bye data<br>
<br>
result = result.upper # probably not what you expect<br>
<br>
if answer.isupper: # always true ("but I entered a lowercase answer")<br>
<br>
<br>
This semester, I ran across a new one that took me quite some time to<br>
figure out what was happening. This started with a habit that too<br>
many students have of wanting to compare boolean values to True/False<br>
literals, rather than just using the boolean as a condition. That is,<br>
students seem drawn to the syntax<br>
<br>
if answer.isupper() == True:<br>
<br>
rather than the preferred<br>
<br>
if answer.isupper():<br>
<br>
These complaints are more of style than substance, as the two<br>
versions are logically equivalent. But then a student came to me<br>
with code that wasn't behaving. The syntax they used was something<br>
akin to<br>
<br>
<br>
if x > y == True:<br>
<br>
however the condition was not being triggered, even when we verified<br>
that x was indeed greater than y. You can try this out with explicit<br>
values as follows.<br>
<br>
>>> if 5 > 4 == True:<br>
... print "Good"<br>
>>><br>
<br>
You will find that the body is not executed. More directly, you can<br>
look at the conditional value directly as<br>
<br>
>>> 5 > 4<br>
True<br>
>>> 5 > 4 == True<br>
False<br>
<br>
This baffled me for an hour before finally seeing what was happening.<br>
I'll leave this as a puzzle for readers (many of whom I'm sure will be<br>
quicker than I was to see the solution). For those who give up, I<br>
offer the following link as a spoiler.<br>
<a href="http://docs.python.org/reference/expressions.html#comparisons" target="_blank">http://docs.python.org/reference/expressions.html#comparisons</a><br>
<br>
With regard,<br>
Michael<br>
<br>
+-----------------------------------------------<br>
| Michael H. Goldwasser, Ph.D.<br>
| Associate Professor<br>
| Director of Computer Science<br>
| Dept. Mathematics and Computer Science<br>
| Saint Louis University<br>
| 220 North Grand Blvd.<br>
| St. Louis, MO 63103-2007<br>
|<br>
| Office: Ritter Hall 108<br>
| Email: <a href="mailto:goldwamh@slu.edu">goldwamh@slu.edu</a><br>
| URL: <a href="http://cs.slu.edu/%7Egoldwasser" target="_blank">http://cs.slu.edu/~goldwasser</a><br>
| Phone: (314) 977-7039<br>
| Fax: (314) 977-1452<br>
<br>
_______________________________________________<br>
Edu-sig mailing list<br>
<a href="mailto:Edu-sig@python.org">Edu-sig@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/edu-sig" target="_blank">http://mail.python.org/mailman/listinfo/edu-sig</a><br>
</blockquote></div><br>