Python Program to Find Area of Rectangle

Python Program to Find Area of Rectangle
Reading Time: 5 minutes

Introduction:

The given Python program is a simple script to calculate the area of a rectangle. The program takes the length and width of the rectangle as input from the user and then uses the provided dimensions to compute the area using the formula area = length * width. The result is then displayed to the user.

Algorithm:

  1. Define the function rectangle_area(length, width) that takes two parameters length and width, representing the dimensions of the rectangle.
  2. Inside the function, calculate the area of the rectangle by multiplying length and width
  3. Return the calculated area.
  4. In the main block of the program (if __name__ == “__main__”:), the script starts executing.
  5. The program prompts the user to enter the length and width of the rectangle using the input()
  6. The user’s input is converted to floating-point numbers using float().
  7. Check if the input length and width are positive (greater than 0).
  8. If either length or width is less than or equal to 0, display an error message asking the user to enter positive values for length and width.
  9. If both length and width are positive, call the rectangle_area function with the provided length and width arguments to calculate the area.
  10. Display the calculated area to the user.

Explanation:

The provided Python script contains a function rectangle_area that calculates the area of a rectangle. The function takes two parameters length and width, both of which should be floating-point numbers representing the dimensions of the rectangle.

In the main block, the script uses the input() function to prompt the user to enter the length and width of the rectangle. The user’s input is converted to floating-point numbers using float() to ensure that the dimensions are treated as decimals.

Next, the program checks if both the length and width values are positive (greater than 0). If either of them is not positive (i.e., less than or equal to 0), the program displays an error message asking the user to enter positive values for the dimensions.

If both length and width are positive, the program proceeds to call the rectangle_area function, passing the provided length and width arguments. The function calculates the area using the formula area = length * width and returns the result.

Finally, the script displays the calculated area of the rectangle using the print() function, completing the program’s execution. If the user enters non-numeric values for the length or width, a ValueError will be raised, and an appropriate error message will be displayed, asking the user to enter numeric values for the dimensions.

To find the area of a rectangle, you need the length and width of the rectangle. You can use the following Python program to calculate the area:

Code :

    
     def rectangle_area(length, width):
    """
    Calculates the area of a rectangle.
    Parameters:
        length (float): Length of the rectangle.
        width (float): Width of the rectangle.
    Returns:
        float: The area of the rectangle.
    """
    return length * width
if __name__ == "__main__":
    try:
        length = float(input("Enter the length of the rectangle: "))
        width = float(input("Enter the width of the rectangle: "))
        if length <= 0 or width <= 0:
            print("Please enter positive values for length and width.")
        else:
            area = rectangle_area(length, width)
            print("The area of the rectangle is:", area)
    except ValueError:
        print("Invalid input. Please enter numeric values for length and width.")

    
   

Input :

    
     Enter the length of the rectangle: 15
Enter the width of the rectangle: 10
    
   

Output :

    
     The area of the rectangle is: 150.0
    
   

Save this code in a Python file (e.g., rectangle_area.py) and run it. The program will prompt you to enter the length and width of the rectangle, and it will calculate and display the area. If you input non-numeric values or negative numbers for the dimensions, it will provide an appropriate error message.