[Tutor] First Try 1.1

John Connors oztriking at hotmail.com
Tue Feb 21 07:19:52 CET 2006


G'day,

Thanks for the input on my lotto number selector program, very much 
appreciated and I learnt a lot. I've (hopefully) cleaned it up a little, and 
expanded it to write the numbers to a text file. I'm sure there must be a 
better way of doing it then the way I have.

I understand that the "else" is not neccessary for the program to work but 
should I include it to show the end of the loop? I guess it's not important 
in a program like this that has only 1 loop but maybe it makes reading more 
complcated programs easier or is the indentation sufficient?



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 choose
how_many_games = int(raw_input('Enter how many games you would like 
generated : '))

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,46), 6)

    # 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

_________________________________________________________________
New year, new job – there's more than 100,00 jobs at SEEK 
http://a.ninemsn.com.au/b.aspx?URL=http%3A%2F%2Fninemsn%2Eseek%2Ecom%2Eau&_t=752315885&_r=Jan05_tagline&_m=EXT



More information about the Tutor mailing list