[Tutor] figured it out

Simonjthecat at aol.com Simonjthecat at aol.com
Fri Apr 23 00:21:49 EDT 2004


Ok... with a little trial and error I figured it out by myself (yay!  this is 
my first working program!)  I'll go ahead and put what I came up w/....  Is 
this the most efficient way?  It seemed kinda weird to define shape as the same 
thing twice but it works.
Here it is.

import sys #for exit command
shape = raw_input("Which shape[1,2,3,quit]? ")

while shape != 'quit':
   print """
   Choose a shape from the list:
   1) Triangle
   2) Square
   3) Circle
   """
   # note must now test for character '1' not 
   # number 1 coz raw_input returns strings not numbers
   if shape == '1':     # a triangle
      ht = input('What is the height of your triangle? ')
      base = input('How long is the base? ')
      print "The triangle's area is: ", 0.5*base*ht

   elif shape == '2':   # square
      side = input("How long are the square's sides? ")
      print "The square's area is: ", side*side

   elif shape == '3':   # a circle 
      rad = input('What radius is your circle? ')
      print "The circle's area is: ", 3.14159*rad*rad

   elif shape == 'quit': 
      sys.exit()

   else:
      print "Sorry, You didn't enter a valid choice"

   shape = raw_input("Which shape[1,2,3,quit]? ")
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20040423/6aad1896/attachment.html


More information about the Tutor mailing list