Newbie question

Tim Peters tim_one at email.msn.com
Mon Jul 26 22:13:32 EDT 1999


[Arnaldo]
> I'm just trying out some examples from the book Learning Python and I run
> into a exercise that is not working for me.
> It's on page 95 , exercise 4b.
> This is the answer the book has
> ===========================================
> l = [1, 2, 4, 8, 16, 32, 64]
> x = 5
> i = 0
> while i < len(l):
>     if 2 ** x == l[i]:
>         print 'at index', i
>         break
>         i = i + 1
> else:
>      print x , 'not found'
> ============================================
>
>  When I run this , python hangs. When I do Ctrl-c , this is what I get
> Traceback (inntermost last):
>     File "powera.py, line 5, in ?
>         if 2 ** x  == l[i]:
>
> What am I doing wrong?

What do you *think* could be going wrong?  The program appears to be running
forever.  How could that happen?  Even if 2**x==l[i] is never true, the loop
is supposed to end as soon as "i < len(l)" is false.  So how can "i <
len(l)" always be true?

Bad hint:  The code above is not the answer the book gives -- although it's
close.

an-hour-of-thinking-will-save-10-seconds-of-reading<wink>-ly y'rs  - tim






More information about the Python-list mailing list