Hey Ben
The problem is quite simple. You're trying to get the lenght of an INTEGER.
That's why it gives you the error. In the position 1 of your list you have an integer and unless you call str(int) you won't be able to get the lenght.
Moving on, there is another way to accomplish the loop using this sentence:
for element in foo:
print len(element)
Regards
Alberto
>From: Ben Markwell <[EMAIL PROTECTED]> >Reply-To: Ben Markwell <[EMAIL PROTECTED]> >To: Python Tutor <tutor@python.org> >Subject: [Tutor] Question regarding the len function of a list while using aloop >Date: Thu, 14 Apr 2005 08:14:12 -0400 > >Could somebody explain to me why the code I used to complete this exercise >doesn't work. >And how do you send an integer to len? > >Thanks > >Ben > >================================== > > >*As an exercise, write a loop that traverses a list and prints the length of >each element. What happens if you send an integer to len? > >*>>> foo = ['spam!', 1, ['brie', 'cheddar', 'swiss'], [1, 2, 3]] > >>> i = 0 > >>> while i < len(foo): >print len(foo[i]) >i = i+1 > > >5 > >Traceback (most recent call last): >File "<pyshell#28>", line 2, in -toplevel- >print len(foo[i]) >TypeError: len() of unsized object >_______________________________________________ >Tutor maillist - Tutor@python.org >http://mail.python.org/mailman/listinfo/tutor_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor