Python code is a simple program designed to calculate and display the area of a rectangle based on user input. It begins by defining a function called calculate_rectangle_area, which takes two parameters representing the length and width of a rectangle. Within this function, the area is computed using the formula area = length * width, and the result is returned. The main part of the program involves interacting with the user. It prompts the user to input the length and width of the rectangle, converts the input to floating-point numbers, and then calls the calculate_rectangle_area function with the provided values. The calculated area is stored in a variable, and the program concludes by printing a formatted string that includes the user-supplied length and width along with the computed area. In essence, the code demonstrates the use of functions for encapsulating a specific task and showcases basic user interaction in a Python program for calculating the area of a rectangle.
Source code
def calculate_rectangle_area(length, width):
area = length * width
return area
# Get input from the user for length and width
length = float(input("Enter the length of the rectangle: "))
width = float(input("Enter the width of the rectangle: "))
# Call the function to calculate the area
area_of_rectangle = calculate_rectangle_area(length, width)
# Display the result
print(f"The area of the rectangle with length {length} and width {width} is: {area_of_rectangle}")
Description
- Function Definition:
- A function named
calculate_rectangle_areais defined, which takes two parameters (lengthandwidth). - Inside the function, it calculates the area of a rectangle using the formula
area = length * widthand returns the result.
- A function named
- User Input:
- The program prompts the user to input the length and width of a rectangle using the
inputfunction. - The inputs are converted to floating-point numbers using
float().
- The program prompts the user to input the length and width of a rectangle using the
- Function Call:
- The
calculate_rectangle_areafunction is called with the user-provided length and width as arguments. - The result is stored in the variable
area_of_rectangle.
- The
- Result Display:
- The program prints the calculated area of the rectangle using a formatted string, including the provided length and width.
- Example Execution:
- When the program is run, it interacts with the user, calculates the area, and displays the result.
Overall, this code is a simple example of how functions can be used to encapsulate a task (calculating the area of a rectangle) and how user input can be processed in a basic Python program.
Screenshot

