[Tutor] Question regarding the len function of a list while using a loop

Ben Markwell benmarkwell at gmail.com
Thu Apr 14 14:14:12 CEST 2005


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20050414/290cccd1/attachment.htm


More information about the Tutor mailing list