[Tutor] Engarde program was: i++

Alan Gauld alan.gauld at btinternet.com
Wed Jun 6 21:39:14 CEST 2007


"scott" <slewin at rogers.com> wrote

> added one in.  I assume that it is good programming practise to 
> close
> all open files when they are not needed anymore?

Yes, its good practice.

> I wrote the is_yes as follows and it seems to work fine.

Almost, but not quite ideal, see below:

> ###################################################
> def is_yes(question):
>     i = 0
>     while i == 0:

You never change i so this is always true.
Therefore you can express that better with

while True:

>         s = raw_input(question)

One wee tweak that might be friendly to your users is to
lower-case the result from raw_input:

         s = raw_input(question).lower()

So now even if the user types 'Y' or 'Yes' it will
get converted to 'y' or 'yes' etc

>         if s == 'y' or s == 'yes':
>             return True
>         elif s == 'n' or s == 'no':
>             return False
>         else:
>             print '\nplease select y, n, yes, or no\n'
> ###################################################

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 




More information about the Tutor mailing list