[Tutor] First Try 1.2

Alan Gauld alan.gauld at freenet.co.uk
Thu Feb 23 22:44:31 CET 2006


Hi John,

Its developing nicely so I'll add some more style issue comments :-)

> import random
>
> # store the /home/mutt/lotto.txt in f
> f = file('/home/mutt/lotto.txt','w')
> # create or replace lotto.txt in my home directory
> f.write('Here are your numbers:\n\n')

I'd move the middle comment up with the one above.
Think of a block of comments commenting on a paragraph of
code, rather than dealing in single lines:

> # store the /home/mutt/lotto.txt in f
> # create or replace lotto.txt in my home directory
> f = file('/home/mutt/lotto.txt','w')
> f.write('Here are your numbers:\n\n')

Now the code is easier sepsarated from the comment.
BUT, is the comment really needed, after all the explicit pathname
shows whgere it is going and the variable tells us where we are storing it.
How much of:

> f = file('/home/mutt/lotto.txt','w')
> f.write('Here are your numbers:\n\n')

will be hard to understand if you come back in a few months?
Comments are best used to explain the *why* of code and let good
variable names and structure describe the how. A concept often
called "Self documenting code"

> # user input for number of games to generate
> how_many_games = int(raw_input('How many games you would like generated? 
> '))
> # user input for number of balls (to allow different lotto games)
> number_of_balls = int(raw_input('\nHow many balls are in the lotto you 
> wish to play? '))
> # user input for how many numbers per game (to allow systems entries)
> how_many_numbers = int(raw_input('\nHow many numbers you would like per 
> game? '))
> # check if there is a power ball
> chk_pwrball = raw_input('Is there a power ball? (y/n) ')

> how_many_games = int(raw_input('How many games you would like generated? 
> '))
> number_of_balls = int(raw_input('\nHow many balls are in the lotto you 
> wish to play? '))
> how_many_numbers = int(raw_input('\nHow many numbers you would like per 
> game? '))
> chk_pwrball = raw_input('Is there a power ball? (y/n) ')

Again comparing these two do the comments add much more information than
the variable names do? And by removing them the code itself becomesa much
more obvious and readable.

> print '\n\nHere are your numbers : \n'
>
> # loop for the number of games selected by user
> for game in range(1, how_many_games + 1):
>
>    # generate 6 random numbers between 1 and 45 inclusive then sort them
>    lotto_numbers = random.sample(range(1,number_of_balls + 1), 
> how_many_numbers)
>    lotto_numbers.sort()
>
>    if chk_pwrball == 'n':
>
>        # Right justified in 3 character width then a tab (\t) then a blank 
> line (\n)
>        print '%3s\t%s\n' % (game, lotto_numbers)
>
>        # write the numbers to lotto.txt
>        save_numbers = 'Game: %3s\t%3s\n' % (game, lotto_numbers)
>        f.write(save_numbers)

The commenting in this section is better spaced but again mostly just
says what the code does. The comment explaining the string format is
probably the most valid comment in the program, it describes what the
format is trying to accomplish which is valid since format strings are
not self evident.

>    if chk_pwrball == 'y':
>        pwrball = random.sample(range(1,number_of_balls +1), 1)
>        print '%3s\t%s   \tPower Ball: %s\n' % (game, lotto_numbers, 
> pwrball)
>        save_numbers = 'Game: %3s\t%s   \tPower Ball: %s\n' % (game, 
> lotto_numbers, pwrball)
>        f.write(save_numbers)
>
> print '\nHope you win!'
> f.write('\nHope you win!')
> f.close()

And interesting that you (coprrectly) chose not to comment this sectoon at 
all

Don't take these comments too personally but it is just a classic case of 
how
you have improved the code clarity to the point where the comments, which
were originally necessary, are now superfluous! This is a common mistake
for beginners so I thought I'd take the opportunity to highlight the issue:

Comments are useful but clearly written code is much better

Alan G 



More information about the Tutor mailing list