Newbie question

Fredrik Lundh fredrik at pythonware.com
Mon Jul 26 22:07:08 EDT 1999


Arnaldo <javanet at sinbad.net> wrote:
> 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'
> ============================================

either you or the LP authors have messed up;
for the loop to ever terminate, the "i = i + 1"
statement should at the "while" level, not in-
side the "if" statement.

try changing the loop to:

    ...
    while i < len(l):
        if 2 ** x == l[i]:
            print 'at index', i
            break
        i = i + 1
    ...

</F>





More information about the Python-list mailing list