Perimeter and Area - Python Programming Exercise

This Python program calculates the perimeter, area, and diagonal of a rectangle based on its width and height. The program prompts the user to enter the dimensions of the rectangle repeatedly until the user enters 0 for the width, signaling the end of the program. The perimeter is calculated by summing the lengths of all four sides of the rectangle, the area is found by multiplying the base (width) and height, and the diagonal is computed using the Pythagorean theorem, which states that the square of the diagonal is equal to the sum of the squares of the width and height. This exercise is a great way to practice using arithmetic operations and conditional loops in Python. It demonstrates how the program can handle repeated calculations, making it ideal for understanding iteration and user input within a loop.

 Category

Python Data Types

 Exercise

Perimeter And Area

 Objective

Develop a Python program to calculate the perimeter, area, and diagonal of a rectangle from its width and height (perimeter = sum of the four sides, area = base x height, diagonal using the Pythagorean theorem). It must repeat until the user enters 0 for the width.

 Example Python Exercise

 Copy Python Code
import math

# Repeat until the user enters 0 for the width
while True:
    # Prompt the user for the width and height
    width = float(input("Enter the width of the rectangle (0 to stop): "))
    
    # If the width is 0, exit the loop
    if width == 0:
        break

    height = float(input("Enter the height of the rectangle: "))

    # Calculate the perimeter (sum of all sides)
    perimeter = 2 * (width + height)

    # Calculate the area (width * height)
    area = width * height

    # Calculate the diagonal using the Pythagorean theorem
    diagonal = math.sqrt(width**2 + height**2)

    # Display the results
    print(f"Perimeter: {perimeter}")
    print(f"Area: {area}")
    print(f"Diagonal: {diagonal}")
    print()  # Blank line for better readability

 Output

Enter the width of the rectangle (0 to stop): 3
Enter the height of the rectangle: 4
Perimeter: 14.0
Area: 12.0
Diagonal: 5.0

Enter the width of the rectangle (0 to stop): 5
Enter the height of the rectangle: 12
Perimeter: 34.0
Area: 60.0
Diagonal: 13.0

Enter the width of the rectangle (0 to stop): 0

 Share this Python Exercise

 More Python Programming Exercises of Python Data Types

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Hex and Bin

    This Python program prompts the user to enter a number and then displays its equivalent values in both hexadecimal and binary formats. The program conti...

  •  Binary Code

    This Python program prompts the user to enter a decimal number and displays its equivalent in binary form. Instead of using the str() function, the prog...

  •  Conditional Logic and Booleans

    This Python program uses the conditional operator to assign a boolean variable named "bothEven" the value "True" if both numbers entered by the user are...

  •  Exception Handling V2

    This Python program prompts the user for a real number and displays its square root. The program uses a "try...except" block to handle any potential err...

  •  Character

    This Python program prompts the user to input three letters and then displays them in reverse order. The program uses basic input and string manipulation techn...

  •  Triangular Shape

    This Python program prompts the user to input a symbol and a width, then displays a triangle of the specified width using that symbol for the inner part...