[Tutor] Suggestions for cleaner code

Jared W jaredw1234567@hotmail.com
Tue Jul 8 20:49:02 2003


Hi, I'm just learning python so my ideas might not be the best way, but I 
think they might be a little cleaner than your program.  I'm using the 
Python for Non-programmers tutorial on the beginner's section of 
www.python.org.  Everything I know about python comes from there.  Here is 
the program I wrote after seeing your program.

#reply to cleaner code e-mail

def printoptions():
	print "Options:"
	print "1 - Greeting"
	print "2 - Discussion"
	print "3 - Question"
	print "4 - Farewell"

selection = 67
while selection != 4:
	if selection == 1:
		print "Hello"
	elif selection == 2:
		print "I should have paid more attention in math class."
	elif selection == 3:
		print "Why did I sleep in math class?"
	elif selection != 4:
		printoptions()
	selection = input("Select an option:")
print "Farewell"

One thing I noticed about your program was that it did not do (or I don't 
think it does) anything if the user does not select one of the 4 choices.  
This could be a problem if the user makes a typing error, or something like 
that happens.  If the person selects 5, I'm not sure what would have 
happened in your program, but in mine, the program would reprint the 
options.  You might want to consider that in your future programs.  Also, 
what is "Core Python?"

Jared

>From: Matt Richardson <marichar@csusb.edu>
>To: tutor@python.org
>Subject: [Tutor] Suggestions for cleaner code
>Date: Tue, 08 Jul 2003 16:16:38 -0700
>
>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
>
>
>
>_______________________________________________
>Tutor maillist  -  Tutor@python.org
>http://mail.python.org/mailman/listinfo/tutor

_________________________________________________________________
Add photos to your messages with MSN 8. Get 2 months FREE*.  
http://join.msn.com/?page=features/featuredemail