[Tutor] Suggestions for cleaner code

Matt Richardson marichar@csusb.edu
Tue Jul 8 19:17:01 2003


Hi all,
I'm starting to get the hang of this, but am looking for some criticism
to keep me on track.  One of the exercises in 'Core Python' is to create
a program that asks a user to select an operation to run, run the
program, then prompt for another operation until an escape command is
given.  After screwing around with it for a couple of days, here's what
I've got:

#!/sw/bin/python

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

loop = 0
while (loop == 0):
    option = input('Option: ')
    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

I'm sure that there are better ways to do this, so I'd like to hear
them.

Thanks,
Matt


-- 
Matt Richardson
Instructional Support Technician
Department of Art
CSU San Bernardino
marichar@csusb.edu