~2 min read • Updated Oct 12, 2025
Program Overview
This Python program reads two numbers and a character from the user.
If the character is R, it calculates the area of a rectangle.
If the character is T, it calculates the area of a triangle using ½ × base × height.
Otherwise, the area is set to zero.
Then, it reads a base salary and a percentage increase, and calculates the final salary.
Python Code:
def calculate_area(a: float, b: float, shape: str) -> float:
if shape == 'R':
return a * b
elif shape == 'T':
return 0.5 * a * b
else:
return 0.0
def calculate_salary(base: float, percent: float) -> float:
bonus = base * percent
return base + bonus
# Read geometry inputs
a = float(input("Enter first number (length or base): "))
b = float(input("Enter second number (width or height): "))
shape = input("Enter shape character (R for rectangle, T for triangle): ").upper()
area = calculate_area(a, b, shape)
print(f"Calculated area: {area:.2f}")
# Read salary inputs
base_salary = float(input("Enter base salary: "))
percent_increase = float(input("Enter percent increase (e.g., 0.10 for 10%): "))
final_salary = calculate_salary(base_salary, percent_increase)
print(f"Final salary with bonus: {final_salary:.0f} Toman")
Sample Output (input: a = 5, b = 4, shape = T, salary = 1,000,000, percent = 0.10):
Calculated area: 10.00
Final salary with bonus: 1100000 Toman
Written & researched by Dr. Shahin Siami