[Tutor] First Try

Todd Maynard python-tutor at toddmaynard.com
Sun Feb 19 12:31:41 CET 2006


and for a belated footnote:

[1] = http://www.python.org/peps/pep-0008.html
Style Guide for python code.

--Todd


On Sunday 19 February 2006 06:27, Todd Maynard wrote:
> Nice Job John.  I made a few comments below on a few things I noticed.....
>
> On Sunday 19 February 2006 05:33, John Connors wrote:
> > G'day :)
> >
> > I started getting sick of reading tutorials so for a bit of a break I set
> > myself the task of writing a program to pick lotto numbers, 6 numbers
> > between 1 and 44 (inclusive). I had done this many years before in basic
> > and I thought back then it would be a simple task but I struck a problem
> > of the random number generator repeating numbers occasionally so I had to
> > check each number against the other and generate another random number if
> > there were duplicates.
> >
> > So I was prepared for the same problem with python but I found that
> > python takes care of that for me so the program would only have to be one
> > line. I decided to make it a little more user friendly and allow the user
> > to pick home many games they want generated. Then I made the output a
> > little easier to read with a few blank lines. Here is what I came up
> > with:
> >
> >
> > import random
> >
> > numberof = int(raw_input('Enter how many games you would like generated
> >
> > :')) #user input for number of games to choose
>
> 10,000 lines of code or 6months later (whichever comes first...) will you
> remember what numberof represents?   Number of times the cow jumped over
> the moon?  I try not to end a var with a prep (and will usually just omit
> it).  I would personally go for num_games_wanted or num_tickets.  (Actually
> to tell the truth I am a mixedCase kinda guy so would really go for
> numGamesWanted or numTickets, but that is not really PEP 8 [1] friendly so
> I am trying to break my ways.)
>
> > print
> > print
> > print "Here are your numbers :"
> > print
>
> If you desire you can use "\n" for return to reduce this all into one line:
>    print "\n\n\Here are your numbers :\n\n"
> No big deal.
>
> > for games in range(1, numberof + 1): #loop for the number of games
> > selected by user
> >     lotto = random.sample(xrange(1,45), 6) #generate 6 random numbers
> > between 1 and 44 inclusive
> >     print games, lotto
> >     print
> >
> > else:
> >     print
> >     print "Hope you win!"
>
> You don't need an else block (you will always enter it, ).
>
> You can just do:
>
> for games in range(1,numberof + 1):
> 	lotto = random.sample(xrange(1,45),6)
> 	print games, lotto
> 	print
> print "\nHope you win!"
>
> > I know this is a very simple program but... could I have done this a
> > better way?
>
> Looks pretty good to me..... If you want to expand this a little more you
> can try:
>
> 1.)  Assign all the lottery tickets to a variable and then print out the
> lottery tickets after you have them all.  ( A list would be helpful
> here...)
>
> 2.)  Break your program into functions so that you can do something like:
>
> (num_tickets_wanted, min_num, max_num) = get_user_input()
> lotto_tickets=generate_tickets(num_tickets_wanted, min_num,max_num)
> print_results(lotto_tickets)
>
> NOTE: It would be pure evil to drop that alll into one line.....
> print_results(generate_tickets(get_user_input()))
>
> I shouldn't have even mentioned that, but you get the idea.....
>
> > John
> >
> > _________________________________________________________________
> > Search for local singles online @ Lavalife - Click here
> > http://a.ninemsn.com.au/b.aspx?URL=http%3A%2F%2Flavalife9%2Eninemsn%2Ecom
> >%2
> > Eau%2Fclickthru%2Fclickthru%2Eact%3Fid%3Dninemsn%26context%3Dan99%26local
> >e%3 Den%5FAU%26a%3D21550&_t=21550&_r=endtext&_m=EXT
> >
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
>
> Have some fun with it....at least until you win the lottery.....
>
> --Todd Maynard



More information about the Tutor mailing list