:
This message was edited by djshadmego at 2002-12-8 17:22:40
:
: I am also trying to learn Python for some back end database storage for future recall into a calendar. I'm trying out Python and Perl actually. I'm stuck in a program that has to have a separate function for the area of a square, the area of a rectangle, and the area of a circle. The program should include a menu interface. I have written it for the most part, but I am trying to cycle it as many times as the user wants to keep going and having a problem with the program following my map. If anyone can help that would be great. Thanks.
I think the main problem is that you want a loop, but you haven't written your code in any form that is conducive to looping. Here's my first shot at cleaning up your code and putting it in a form that demonstrates the idea of a user-terminated endless loop:
pi = 3.14
def hello():
print 'Hello!'
def areasq(width):
return width**2
def arearec(width,height):
return width*height
def areacir(radius):
return pi*radius**2
def prompt_for_name():
return raw_input('Your name please: ')
def print_welcome(name):
print 'Welcome,',name
def main_menu():
print
print "I can help you find the area of a square,",
print "a rectangle, or a circle."
print "Which one would you like to know? "
return raw_input("Enter your selection: ")
if __name__ == '__main__':
hello()
username = prompt_for_name()
print_welcome(username)
continue_looping = True
while(continue_looping):
#print main menu
selection = main_menu()
#process selection
if selection == 'square':
w = input("Width: ")
while w <= 0:
print "This number must be a positive number. (Anything above '0')"
w = input("Width: ")
print "The area of a Square is the width times itself or w**2"
print "Width = ",w," so the area =",areasq(w)
print
elif selection == 'rectangle':
w = input("Width: ")
while w <= 0:
print "This number must be a positive number. (Anything above '0')"
w = input("Width: ")
h = input("Height: ")
while h <= 0:
print "This must be positive also."
h = input("height: ")
print "The area of a rectangle is the width times the height or w*h"
print "Width =",w,"Height =",h," so the area =",arearec(w,h)
print
elif selection == 'circle':
r = input("Radius? ")
while r <= 0:
print "Alas, this must be a positive number. Please try again."
r = input("Radius: ")
print "The area is pi,",pi,"times the radius squared or 3.14*r**2"
print "Pi =",pi,"Radius = ",r," so the area of the circle =",areacir(r)
#ask user to repeat
repeat = raw_input("Would you like to do another one? ")
if repeat.upper() not in ['Y', 'YES']:
continue_looping = False
else:
print "Have a nice day,", username, "!"