Re: [Tutor] First Try 1.2
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 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] First Try 1.2
G'day Kent, Thanks for the input. >The comments don't really add anything to the program. Comments that >restate exactly what the code is doing are not that helpful. The code >should be clear by itself. As a beginner they may seem like a helpful >crutch but you will get better at reading the code. Very good point. I have to go away for 3 weeks in a couple of days and I was trying to leave helpful hints to myself in the code so I won't forget what I have learnt. But you are right, I got carried away and most were just getting in the way. John _ Shopping made easy @ tradingpost.com.au http://a.ninemsn.com.au/b.aspx?URL=http%3A%2F%2Fwww%2Etradingpost%2Ecom%2Eau%2F%3Freferrer%3DnmsnHMetagv1&_t=753082530&_r=emailtagline&_m=EXT ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] First Try 1.2
John Connors wrote: > 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') > The comments don't really add anything to the program. Comments that restate exactly what the code is doing are not that helpful. The code should be clear by itself. As a beginner they may seem like a helpful crutch but you will get better at reading the code. For example, how_many_games = int(raw_input('How many games you would like generated? ')) is well-written and expressive. With the helpful variable name and the input prompt it's pretty easy to figure out what it does. Adding the comment # user input for number of games to generate actually obscures the code without adding to its comprehensibility. A better choice might be to have one comment for the whole block of user input code, something like # Get required user input Good comments describe the code at a higher level than the code itself, or explain tricky bits. Comments that give a blow-by-blow account of the code just get in the way. I will often break my code into small chunks with a comment before each chunk that says what it does. Then it is very easy to skim the code and skip sections I'm not interested in. > # 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) ') Here is another reason why it is a bad idea to just describe the code in the comments - often the code changes and the comment doesn't. Now you have an inaccurate comment which is worse than no comment at all. > # 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) > print '\nHope you win!' Good luck! Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] First Try 1.2
G'day, Added a power ball option to the lotto program today. The program does way more then I ever intended when I started it so I'll try a new project. Maybe I'll come back to it when I try out Tkinter and try to give it a GUI. Anywayhere's my latest effort. 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') # 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) ') 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) 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() John _ Shopping made easy @ tradingpost.com.au http://a.ninemsn.com.au/b.aspx?URL=http%3A%2F%2Fwww%2Etradingpost%2Ecom%2Eau%2F%3Freferrer%3DnmsnHMetagv1&_t=753082530&_r=emailtagline&_m=EXT ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] First Try 1.2
G'day :) I've added a little more to the program. It now sorts the game numbers from lowest to highest which makes filling out the tickets a lot easier, I've been putting off doing this because I wasn't sure how to go about it but once I started looking I found python does it all for me with .sort() Also added some extra user input so different types of lotto games can be played. Found a glaring bug...the program didn't pick the winning numbers for me and I'm now $6 poorer! Next on the to do list is add an option for a power ball, I actually did this tonight but I had 'if' statements all over the place. Which made the code unreadable so I'm going to work on a different way of doing it. I hope posting my efforts in here is ok. I learnt a lot after posting the first version of it but if I'm doing the wrong thing please tell me and I'll desist. import random # create or replace lotto.txt in my home directory file('/home/mutt/lotto.txt','w').write('Here are your numbers:\n\n') # 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? ')) 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 lotto_numbers = random.sample(xrange(1,number_of_balls + 1), how_many_numbers) lotto_numbers.sort() # Right justified in 3 character width then a tab (\t) then a blank line (\n) print '%3s\t%s\n' % (game, lotto_numbers) # append the numbers to lotto.txt file('/home/mutt/lotto.txt','a').write('Game: ') file('/home/mutt/lotto.txt','a').write(str(game)) file('/home/mutt/lotto.txt','a').write('') file('/home/mutt/lotto.txt','a').write(str(lotto_numbers)) file('/home/mutt/lotto.txt','a').write('\n') print '\nHope you win!' file('/home/mutt/lotto.txt','a').write('\n\nHope you win!') John _ Search for local singles online @ Lavalife - Click here http://a.ninemsn.com.au/b.aspx?URL=http%3A%2F%2Flavalife9%2Eninemsn%2Ecom%2Eau%2Fclickthru%2Fclickthru%2Eact%3Fid%3Dninemsn%26context%3Dan99%26locale%3Den%5FAU%26a%3D21550&_t=21550&_r=endtext&_m=EXT ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor