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:
- Define the function rectangle_area(length, width) that takes two parameters length and width, representing the dimensions of the rectangle.
- Inside the function, calculate the area of the rectangle by multiplying length and width
- Return the calculated area.
- In the main block of the program (if __name__ == “__main__”:), the script starts executing.
- The program prompts the user to enter the length and width of the rectangle using the input()
- The user’s input is converted to floating-point numbers using float().
- Check if the input length and width are positive (greater than 0).
- 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.
- If both length and width are positive, call the rectangle_area function with the provided length and width arguments to calculate the area.
- 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.