[Tutor] Suggestions for cleaner code

Matt Richardson marichar@csusb.edu
Tue Jul 8 21:08:05 2003


On Tue, 2003-07-08 at 17:48, Jared W wrote:
> 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
> 
The user error instance is a good point.  I should have been more
explicit in the name of the text I am using, 'Core Python Programming',
by Wesley Chun.  Picked it up new on amazon for $15.  I started with the
same tutorial you are using, but needed something that was going to hold
my hand even more.

Matt

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