Here's a simple Python program that calculates the area of different shapes:
python
def calculate_circle_area(radius):
"""Calculate the area of a circle given its radius."""
pi = 3.14159
return pi * radius ** 2
def calculate_rectangle_area(length, width):
"""Calculate the area of a rectangle given its length and width."""
return length * width
def calculate_triangle_area(base, height):
"""Calculate the area of a triangle given its base and height."""
return 0.5 * base * height
def main():
print("Shape Area Calculator")
print("-" * 20)
while True:
print("\nChoose a shape:")
print("1. Circle")
print("2. Rectangle")
print("3. Triangle")
print("4. Exit")
choice = input("\nEnter your choice (1-4): ")
if choice == "1":
radius = float(input("Enter the radius of the circle: "))
area = calculate_circle_area(radius)
print(f"The area of the circle is: {area:.2f}")
elif choice == "2":
length = float(input("Enter the length of the rectangle: "))
width = float(input("Enter the width of the rectangle: "))
area = calculate_rectangle_area(length, width)
print(f"The area of the rectangle is: {area:.2f}")
elif choice == "3":
base = float(input("Enter the base of the triangle: "))
height = float(input("Enter the height of the triangle: "))
area = calculate_triangle_area(base, height)
print(f"The area of the triangle is: {area:.2f}")
elif choice == "4":
print("Thank you for using the calculator. Goodbye!")
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()
This program demonstrates several fundamental Python concepts. It defines three functions to calculate the areas of different shapes, each taking the appropriate parameters and returning the calculated area. The main function creates an interactive menu system using a while loop that continues until the user chooses to exit.
The program uses input() to get user choices and shape dimensions, converts string inputs to floats for mathematical calculations, and uses f-strings for formatted output. The if-elif-else structure handles different menu choices, and the program includes basic error handling for invalid menu selections.
To run this program, save it as a .py file (like shape_calculator.py) and execute it using Python. The program will display a menu, let you choose a shape, ask for the necessary measurements, and then calculate and display the area.