[Tutor] newbie 'while' question

Zak Arntson zak@harlekin-maus.com
Mon Jun 30 15:16:03 2003


> Hi,
>
> while x <= NumOfChar:
>     dosomestuff
>
> Or maybe I'm off track here, too.

Well, yes and no. It depends on whether x <= NumOfChar will ever be false,
and break you out of the while loop. Two things to remember about while
loops:
a) They're dangerous, and
b) You (almost) always want an "escape route." In your case, definitely so.

Instead of considering integers, try modifying the string itself. I don't
want to give the answer away, so I'll try to get you started with a
different example:

>>> s = ''
>>> while s:
        print 'yup'

Notice how you don't see 'yup' printed? That's because '' (an empty
string) is just as good as false. Did you see my prior post, about
accessing part of a string?

>>> s = 'frog'
>>> s[0]
'f'
>>> s[2:]
'og'
>>> s[6:]
''

There's a few hints towards my solution (which, admittedly, may not be the
best).


---
Zak Arntson
www.harlekin-maus.com - Games - Lots of 'em