[Tutor] newbie 'while' question

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Mon Jun 30 18:44:32 2003


> > In mathematical terms, we'd say that we'd like a half-open range,
> >
> >
> >          0  <=  x   < length of "Matthew Ricahrdson"
> >
> >
> > And we'll find that the equivalent expression in Python is very similar to
> > this.  *grin*
> >
> >
> > I hope this gives some hints on how to correct the while 'condition'
> > expression.  Good luck!
>
> I knew I should have paid more attention in math...


You did have the right approach.  That is, you approached it from the
left:

    while x >= 0:                   #  0 <= x

You just had to have the right approach.  *grin*

    while x < len(s):               #  x < len(s)





By the way, it turns out that we can do both directions at the same time:

    while 0 <= x < len(s):          #  0 <= x and x < len(s)


which is a safe way to go about it.  Of course, only a few programmers I
know will ever write the condition like this.  *grin* Most will just
write:

    x < len(s)

and just assume that x is nonnegative.  Crazy programmers.  *grin*



Good luck to you!