[Tutor] use while loop

Simonjthecat at aol.com Simonjthecat at aol.com
Thu Apr 22 23:13:43 EDT 2004

Ok.  I know this is really simple, but it's my first program so anybody can 
pobably answer it for me.  This program finds the area of different shapes.  
Right now, however, it only goes through one time and then exits the program.  I 
want to put it in a loop so that after one area is given it will start over 
and ask what shape you want again.  I figured out how to put it in a for loop 
and get it to execute a given number of times.  I'm assuming I need it in a 
while loop but just can't do it.  So anyway, here's what I've got so far.

import sys #for exit command

print """
Choose a shape from the list:
1) Triangle
2) Square
3) Circle
shape = raw_input("Which shape[1,2,3,quit]? ") 
# 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': 

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

I know this has a real simple fix.  
Thanx for the help

Simon the Cat
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20040422/f546cf2e/attachment.html

More information about the Tutor mailing list