[Tutor] ....and another thing!

charlie derr wrong@crosswinds.net
Fri, 3 Aug 2001 16:46:09 -0400

My guess is that the line:


is incorrect.  I think you are never picking the first card, and you are
occasionally picking outside the range of schmack[].  Remember that python
indexes start at 0, not at 1.  I also don't really understand why you use
the string.atoi() construction (but maybe there's a reason for it).  Try

	choose=random.randint(0, total_cards - 1)

(or if there's a reason for string.atoi() that i'm missing):

	choose=random.randint(0, string.atoi(`total_cards`) - 1)

good luck,

+In the following:
+mport random
+import string
+while counter <10:
+	total_cards=len(schmack)
+	choose=random.randint(1,string.atoi(`total_cards`))
+	chosen=schmack[choose]
+	del schmack[choose]
+	print chosen
+	counter=counter+1
+I will get the occasional:
+Traceback (most recent call last):
+  File "reader.py", line 9, in ?
+    chosen=schmack[choose]
+IndexError: list index out of range
+>Exit code: 1
+It seems to me that:
+should recalculate the number of items in the list with each
+iteration of the
+loop. Since there are 72 items in the list, and one item is
+removed from the
+list at the end of every iteration, it stands to reason that the
+list index
+should be accurately recalculated by len(). Where am I going wrong?
+Tutor maillist  -  Tutor@python.org