Lists

Johann Hibschman johann at physics.berkeley.edu
Tue Apr 18 13:00:51 EDT 2000


Daley, MarkX writes:

> 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

No, it's iterating in order.
On the first step,
     item = -5, a[item] = 1, and 5/1 = 1
then item = -4, a[item] = 2, and 5/2 = 2
then item = -3, a[item] = 3, and 5/3 = 1

I'm guessing that you either:
  1. Really want "b/item" not "b/a[item]"
or
  2. Don't understand negative indexing.  A negative index to a list
     counts from the back of the list front.


-- 
Johann Hibschman                           johann at physics.berkeley.edu



More information about the Python-list mailing list