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

Max Noel maxnoel_fr at yahoo.fr
Thu Apr 14 14:26:31 CEST 2005


On Apr 14, 2005, at 14:14, Ben Markwell wrote:

> 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?

	Well, I think you've successfully completed that exercise. len() 
doesn't work on integers because integers don't have a length.

	Think of it that way:
- "foo" is a string of length 3 (it contains 3 elements (characters))
- [1, 2, 3] is a list of length 3 (it contains 3 elements)
- {'a': 1, 'b': 2, 'foo': 5} is a dict of length 3 (it contains 3 
elements)
- 100 is an integer. How, as a human being, would you define its 
"length"? How many elements does it contain? It doesn't make sense, 
does it?

	Well, you have it. Numbers (ints, floats...) don't have a length. 
Therefore, calling len() on an integer is a non-sense that results in 
an exception.

-- Max
maxnoel_fr at yahoo dot fr -- ICQ #85274019
"Look at you hacker... A pathetic creature of meat and bone, panting 
and sweating as you run through my corridors... How can you challenge a 
perfect, immortal machine?"




More information about the Tutor mailing list