Lists
Fredrik Lundh
effbot at telia.com
Tue Apr 18 12:45:20 EDT 2000
Daley, MarkX <markx.daley at intel.com> wrote:
> This feels like a real newbie question, but here is the code that is
causing
> my question:
>
> # Learning exceptions
>
> def test():
> a = [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]
> print a
> b = 5
> for item in a:
> try:
> print b / a[item]
> except ZeroDivisionError:
> pass
>
> Here is the output of this code under IDLE:
>
> [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]
> 5
> 2
> 1
> 1
> 1
> -1
> -2
> -2
> -3
> -5
>
> Why is the list being processed in reverse? The exception works fine, but
> the math is backwards. I don't recall this happening before.
note that you divide by a[item], not by item.
try printing item and a[item] in the loop and see if you
can figure out what's going on...
(where did you find this script, btw? "100 obfuscated
python scripts for beginners?" ;-)
</F>
More information about the Python-list
mailing list