[Tutor] List exercise
Larry Holish
ljholish@speakeasy.net
Fri Feb 7 12:21:31 2003
On Fri, Feb 07, 2003 at 08:30:42PM +0800, Anwar.Lorenzo@gmx.net wrote:
> Hi
> I'm trying to solve the exercise:
>
> As an exercise, write a loop that traverses the previous list
> (['spam!', 1, ['Brie', 'Roquefort', 'Pol le Veq'], [1, 2, 3]]) and
> prints the length of each element. What happens if you send an
> integer to len? (exercise from 8.3)
>
> from the ebook How To Think Like A Computer Scientist: Learning with
> Python.
>
> I've coded a function to solve this but it doesn't seem to work. IDLE
>
> just hangs when I execute this function.
>
> Here's my solution:
>
> def exerSize():
> exer = ['spam!', 1, ['Brie', 'Roquefort', 'Pol le Veq'], [1, 2, 3]]
> i = 0
> while i < len(exer):
> len(exer[i])
> i = i + 1
The type() builtin may also be useful for this problem:
import types
if type(mylist) == types.ListType:
print 'mylist is a list'
--
Larry Holish
<ljholish@speakeasy.net>