Mailing List Archive

Newbie question
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?
Thanks I lot
Newbie question [ In reply to ]
Arnaldo <javanet@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>
Newbie question [ In reply to ]
[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
Newbie question [ In reply to ]
On Tue, 27 Jul 1999, Fredrik Lundh 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.

It's a very understandable problem -- there is a page break before the
line in question, and I don't blame Arnaldo for not having taken his ruler
and counting spaces. Alas, there was little we could do in the production
process to avoid all the page breaks in Python code. Sorry you wasted
some time on that one, Arnaldo.

python-code-wasn't-designed-for-typesetting-in-more-ways-than-one'ly y'rs,

--david ascher