[Tutor] Suggestions for cleaner code

Matt Richardson marichar@csusb.edu
Wed Jul 9 11:33:02 2003


Thanks for the suggestions, it's looking cleaner already.  I haven't
implemented the dictionary yet, but I have incorporated some of the
suggestions posted:

#!/sw/bin/python

print 'Choose one of the following: '
print '1  Greeting'
print '2  Discussion'
print '3  Question'
print '4  Farewell'

while True:
    option = int(raw_input('Option: '))
    if option == 1:
        print 'Hello'
    elif option == 2:
        print 'I should have paid more attention in math classes.'
    elif option == 3:
        print 'Why did I sleep through class?'
    elif option == 4:
        print 'Farewell'
        break

Got rid of the unnecessary 'loop' variable I was using and a level of if
statements.  The tasks the options perform here are obviously trivial,
but I have a real use for this type of program so getting it nice and
clean is important to me.  I manage four computer labs on a part-time
basis (budgets constraints make for strange job descriptions) and need
to make a program that gives the user (instructor or other tech) a
simple interface to run a more complex suite of imaging and updating
programs.

thanks for the help,
Matt


On Tue, 2003-07-08 at 17:52, Jeff Shannon wrote:
> Kalle Svensson has given some good suggestions, and if you follow his 
> dictionary hints then this bit of code becomes redundant, but I figured 
> I'd point this out anyhow...
> 
> >    if (1 <= option <= 3):
> >        if option == 1:
> >            print 'Hello'
> >        elif option == 2:
> >            print 'I should have paid more attention in math classes.'
> >        elif option == 3:
> >            print 'Why did I sleep through class?'
> >    else:
> >        print 'Farewell'
> >        loop = 1
> >
> 
> Here you are checking to see if option is between 1 and 3, and then 
> checking which number it is.  You could do this just as well with a 
> single level of if/elif, instead of two levels:
> 
>     if option == 1:  [...]
>     elif option == 2: [...]
>     elif option == 3: [...]
>     else:        [...]
> 
> Also, as a minor point, one could argue that you're not being completely 
> truthful to your users -- you claim that 4 will quit, but really 
> *anything* except 1, 2, or 3 will quit.  Someone who accidentally hits a 
> key other than 1-4 might be unhappy that the program exited rather than 
> giving them another chance...  This can be solved by either limiting the 
> quit to 4 and having any other input cause a new cycle through the loop 
> (just change that last else into an elif), or by changing your message 
> to inform users (instead of  '4 Farewell', say something like 'Any other 
> key:  Farewell').
> 
> Jeff Shannon
> Technician/Programmer
> Credit International
> 
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
-- 
Matt Richardson
Instructional Support Technician
Department of Art
CSU San Bernardino
marichar@csusb.edu