Re: [Tutor] First Try 1.1

2006-02-21 Thread Alan Gauld
> 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?

The indentation is fine, thats what its there for! :-)

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

opening and closing(implicitly) the file each time is both slow and
expensive on computer resources. Just store the open file in a
variable like this:

output = file('/home/mutt/lotto.txt','w')
output.write('a first string\n')
output.write('a second string\n')
output.write('a final string\n')
output.close()

Note that you need to explicitly add the newline (\n) at the end
of each string. And explicitly closing the file is considered good
practice.

>lotto_numbers = random.sample(xrange(1,46), 6)

You don't really need xrange, a simple range will suffice,
but it does no harm.

># 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')

And you can use string forematting to create the string before writing:

info = "Game: %3d\t%3d\n" % (game, lotto_numbers)
output.write(info)

HTH,

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] First Try 1.1

2006-02-21 Thread Kent Johnson
John Connors wrote:
> 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?

'else' in a for loop has a very specific meaning and usage - the else 
clause is executed only if the for loop completes normally, without 
executing a break statement. This is useful when you have a loop that is 
searching for some condition, and you want to execute some default code 
if the condition is never met. For example, a simple loop to search a 
list for a value and print a result could look like this (note: this is 
NOT the best way to solve this problem, it is just a simple example of 
for / else):

  >>> def search(value, lst):
  ...   for item in lst:
  ... if value == item:
  ...   print 'Found', value
  ...   break
  ...   else:
  ... print value, 'not found'
  ...
  >>> lst = range(10)
  >>> search(3, lst)
Found 3
  >>> search(11, lst)
11 not found

for / else is very handy in this situation and should not be used just 
to show that the loop is over.

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor