silly idea - interesting problem

Steve Holden sholden at holdenweb.com
Sun Oct 21 17:46:31 CEST 2001


"scott" <smarsh at hotmail.com> wrote ...
>
[ ... handles nit-picks ...]
>
> Since we are nit-picking, the spaces are actually between each letter
> ;-)
> The point I was trying to make was that spraying a new Python user with
> marginally readable code might be good for your ego but not much help
> for learning the basics. I admit that if my code actually met the
> requirements my argument would have been a bit more persuasive <my first
> *wink* god help me now> :-)
>
> Here is the (as specified) no space version:
>
> firstWord = 'test'
> secondWord = 'dust'
> endString = ''
> i = 0
> while i < len(firstWord):
>     endString = endString + firstWord[i] + secondWord[i]
>     i = i+1
> print endString
>

Surely much simpler and, dare I suggest, rather more pythonic also, to use a
"for" loop? As in:

...
for i in range(len(firstWord)):
    endString += firstWord[i] + secondWord[i]
...

Of course one would also ideally perform some tests, such as "if
len(firstWord) != len(secondWord):"

before hand, or at least cope with any exceptions raised by the lack of such
checks under certain circumstances. We'll leave that as an exercise to the
OP, I guess.

regards
 Steve
--
http://www.holdenweb.com/








More information about the Python-list mailing list