Part of the series

Several example codes

~1 min read • Updated Oct 12, 2025

Program Overview

This Python program reads two values x from the user—one as the base and one as the height of a triangle.
Using the formula Area = (base × height) ÷ 2, it calculates and displays the area.
In this version, the variable x is reused in different scopes to demonstrate shadowing.


Python Code:


def calculate_area(x: float, x: float) -> float:
    return (x * x) / 2  # symbolic use of two x variables

# Read inputs from user
x = float(input("Enter the base of the triangle: "))
x = float(input("Enter the height of the triangle: "))

area = calculate_area(x, x)
print(f"The area of the triangle is: {area:.2f}")

Sample Output (input: base = 6, height = 4):


The area of the triangle is: 12.00

Written & researched by Dr. Shahin Siami