This is the first mini-project I have completed by myself with Python 2.7. I wrote a simple program that calculates the area of different two-dimensional shapes and it works. I want to know what you think of it and also want to know how it can be improved and if I did something wrong, so this is my code:
def circle(pi,radius):
area_circle = pi * (radius ** 2)
print " the area of your circle equals %s" % (area_circle)
def rectangle(length, width):
area = length * width
print "The area of your rectangle equals %s" %(area)
def triangle(height, base):
area_triangle = height * base * 0.5
print "the area of your triangle equals %s" % (area_triangle)
def square(length):
area_square = length ** 2
print "the area of your square equals %s" % (area_square)
shapes = ["square", "rectangle", "triangle", "circle"]
print "Area calculator!"
print "Made by Ahmed Fawzy"
print "-------------------------------"
def calculation():
print "please type the name of your shape"
print "(Square, Rectangle, Triangle, Circle)"
user_shape = raw_input()
if user_shape == shapes[0]:
square(length = float(raw_input("please type the length of the square.")))
elif user_shape == shapes[1]:
rectangle(length = float(raw_input("please type the length of the rectangle.")), width = float(raw_input("please type the width of the rectangle.")))
elif user_shape == shapes[2]:
triangle(height = float(raw_input("please type the height of the triangle.")), base = float(raw_input("please type the base length of the triangle.")))
elif user_shape == shapes[3]:
circle(3.14, radius = float(raw_input("please type the radius of the circle.")))
else:
print "That's not in the choices!, Try again."
calculation()
calculation()
choice = raw_input("Would you like to calculate the area of a different shape?(yes/no)")
while choice == "yes":
print "---------------------"
calculation()
choice = raw_input("Wanna go again?")
There's one more thing I'd like to add but don't know how, I want to make it infinite loop when the user types an answer different than 'yes' or 'no' to the "would you like to calculate the area of a different shape?" , until the user types the correct answer which is yes or no then the loop breaks and it calculates again, any tips?
while True
loop and thebreak
keyword. \$\endgroup\$