Vamos a hacer un programa en Python para calcular áreas de algunas formas matemáticas.
Ejemplo:
Input: shape name = "Rectangle" length = 10 breadth = 15 Output: Area: 150 Input: shape name = "Square" side = 10 Output: Area: 100
Acercarse:
En este programa, le pediremos al usuario que ingrese el nombre de la forma. Si existe en nuestro programa procederemos a encontrar el área de la forma ingresada de acuerdo a sus respectivas fórmulas. Si esa forma no existe, imprimiremos “¡Lo siento! No podemos encontrar esta forma. mensaje en la pantalla.
A continuación se muestra la implementación:
Python3
# define a function for calculating # the area of a shapes def calculate_area(name):\ # converting all characters # into lower cases name = name.lower() # check for the conditions if name == "rectangle": l = int(input("Enter rectangle's length: ")) b = int(input("Enter rectangle's breadth: ")) # calculate area of rectangle rect_area = l * b print(f"The area of rectangle is {rect_area}.") elif name == "square": s = int(input("Enter square's side length: ")) # calculate area of square sqt_area = s * s print(f"The area of square is {sqt_area}.") elif name == "triangle": h = int(input("Enter triangle's height length: ")) b = int(input("Enter triangle's breadth length: ")) # calculate area of triangle tri_area = 0.5 * b * h print(f"The area of triangle is {tri_area}.") elif name == "circle": r = int(input("Enter circle's radius length: ")) pi = 3.14 # calculate area of circle circ_area = pi * r * r print(f"The area of circle is {circ_area}.") elif name == 'parallelogram': b = int(input("Enter parallelogram's base length: ")) h = int(input("Enter parallelogram's height length: ")) # calculate area of parallelogram para_area = b * h print(f"The area of parallelogram is {para_area}.") else: print("Sorry! This shape is not available") # driver code if __name__ == "__main__" : print("Calculate Shape Area") shape_name = input("Enter the name of shape whose area you want to find: ") # function calling calculate_area(shape_name)
Producción:
Calculate Shape Area Enter the name of shape whose area you want to find: rectangle Enter rectangle's length: 10 Enter rectangle's breadth: 15 The area of rectangle is 150.
Publicación traducida automáticamente
Artículo escrito por zetareticuli34 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA