my first software

Leo Kislov Leo.Kislov at gmail.com
Fri Oct 27 02:22:55 EDT 2006


FS.Jason at gmail.com wrote:
> I am a beginner of programming and started to learn Python a week ago.
> last 3 days, i write this little tool for Renju.if you have any advice
> on my code,please tell me

>     s = ''
>
>     for i in range (0,len(done) - 1):
>         s = s +str(done[i][0]) + str(done[i][1]) + '\n'
>     s = s + str(done[len(done) - 1][0]) + str(done[len(done) - 1][1])
>
>

This is easier to do with a generator comprehension and join method:

s = '\n'.join(str(item[0]) + str(item[1]) for item in done)


>     for i in range (0, len(s)):
>         x = s[i][0]
>         .....
>         if i%2 == 0:
>         ....

There is a builtin function enumerate for this case, IMHO it's slightly
easier to read:

for i, item in enumerate(s)
    x = item[0]
    ...
    if not i%2:
    ...

>     if len(done) != 0 and beensaved == 0 and askyesno(...):
>             saveasfile()

It's a personal matter, but usually python programmers treats values in
boolean context directly without comparison:

if done and not beensaved and askyesno(...):

The rules are documented here: http://docs.python.org/lib/truth.html .

  -- Leo




More information about the Python-list mailing list