[Tutor] List exercise
Anwar.Lorenzo@gmx.net
Anwar.Lorenzo@gmx.net
Fri Feb 7 08:38:04 2003
>
> On Friday, February 7, 2003, at 07:30 AM, Anwar.Lorenzo@gmx.net wrote:
>
> > 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
>
> Hi Anwar,
>
> I think the reason your program hangs is because you have an infinite
> loop. The "while" loop says "while i is less than the length of exer",
> and since i is zero, this loop is always true, and never stops. If you
> were thinking that your i variable would increment on each iteration of
> the loop (by using "i = i + 1"), the problem is that that line is
> outside of the loop. So that line only executes after the loop is done
> iterating, but because it's an infinite loop, it never stops.
>
> Try putting that i = i + 1 line inside the while loop and see what your
> program does.
>
> while i < len(exer):
> len(exer[i])
> i = i + 1 # all you have to do is indent it to line up with
> the previous line
>
>
>
> Erik
>
>
>
>
>
> --
> Erik Price
>
> email: erikprice@mac.com
> jabber: erikprice@jabber.org
>
I've tried indenting that line (i = i + 1) but I got this error:
Traceback (most recent call last):
File "<pyshell#2>", line 1, in ?
exerSize()
File "<pyshell#1>", line 5, in exerSize
len(exer[i])
TypeError: len() of unsized object