Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/marioaje/Python/llms.txt

Use this file to discover all available pages before exploring further.

These five exercises reinforce everything introduced in Week 2: arithmetic operators, conditional statements (if/elif/else), user input with input(), and basic algorithmic thinking. Work through each problem on your own before expanding the solution — the real learning happens in the struggle.
input() always returns a string. If you need to do arithmetic with a value the user types in, you must convert it first:
age   = int(input("Enter your age: "))     # whole numbers
price = float(input("Enter the price: "))  # decimal numbers
Forgetting this conversion is one of the most common beginner mistakes — Python will raise a TypeError if you try to compare or add a string to a number.

Exercise 1 — Investment Profit

Problem statement:
A person has $100,000 and decides to invest $70,000 in mortgage bonds at 5% monthly interest, and the remainder ($30,000) in a term deposit at 10% monthly interest. How much money will this person earn after one month?
ejercicio1.py
# Investment amounts
total        = 100_000
bonds        = 70_000
term_deposit = total - bonds   # 30,000

# Monthly returns
bonds_profit        = bonds        * 0.05
term_deposit_profit = term_deposit * 0.10

total_profit = bonds_profit + term_deposit_profit

print(f"Bonds profit       : ${bonds_profit:,.2f}")
print(f"Term deposit profit: ${term_deposit_profit:,.2f}")
print(f"Total profit       : ${total_profit:,.2f}")
Expected output:
Bonds profit       : $3,500.00
Term deposit profit: $3,000.00
Total profit       : $6,500.00
The person earns $6,500 after one month.

Exercise 2 — Shopping Discount vs. Tax Order

Problem statement:
You are going to shop for ₡12,000 at a store and the seller offers you a discount of ₡1,000. Show which final price is cheaper: applying the discount first, then 13% tax, or applying the 13% tax first, then the discount.
ejercicio2.py
price    = 12_000
discount =  1_000
tax_rate =  0.13

# Option A: discount first, then tax
option_a = (price - discount) * (1 + tax_rate)

# Option B: tax first, then discount
option_b = price * (1 + tax_rate) - discount

print(f"Option A (discount → tax): ₡{option_a:,.2f}")
print(f"Option B (tax → discount): ₡{option_b:,.2f}")

if option_a < option_b:
    print("Option A is cheaper: apply the discount first.")
elif option_b < option_a:
    print("Option B is cheaper: apply the tax first.")
else:
    print("Both options give the same final price.")
Expected output:
Option A (discount → tax): ₡12,430.00
Option B (tax → discount): ₡12,560.00
Option A is cheaper: apply the discount first.
Applying the discount before tax saves ₡130 because the 13% tax is calculated on a smaller base.

Exercise 3 — Find the Larger of Two Values

Problem statement:
Write an algorithm that reads two values, determines the larger one, and prints it with an identification message.
ejercicio3.py
a = float(input("Enter the first value:  "))
b = float(input("Enter the second value: "))

if a > b:
    print(f"The larger value is A: {a}")
elif b > a:
    print(f"The larger value is B: {b}")
else:
    print(f"Both values are equal: {a}")
Example run:
Enter the first value:  45
Enter the second value: 72
The larger value is B: 72.0

Exercise 4 — Invoice Total

Problem statement:
Write an algorithm that reads the following values: price, tax (as a percentage), and discount (as a fixed amount), then calculates the total invoice value. The discount is applied to the price first; then tax is added.
ejercicio4.py
price    = float(input("Enter the price:             "))
tax_pct  = float(input("Enter the tax percentage:    "))
discount = float(input("Enter the discount amount:   "))

# Apply discount first, then add tax
subtotal = price - discount
tax_amt  = subtotal * (tax_pct / 100)
total    = subtotal + tax_amt

print(f"\n--- Invoice ---")
print(f"Price           : ₡{price:>10,.2f}")
print(f"Discount        : ₡{discount:>10,.2f}")
print(f"Subtotal        : ₡{subtotal:>10,.2f}")
print(f"Tax ({tax_pct}%)    : ₡{tax_amt:>10,.2f}")
print(f"Total           : ₡{total:>10,.2f}")
Example run:
Enter the price:             15000
Enter the tax percentage:    13
Enter the discount amount:   500

--- Invoice ---
Price           : ₡  15,000.00
Discount        : ₡     500.00
Subtotal        : ₡  14,500.00
Tax (13.0%)    : ₡   1,885.00
Total           : ₡  16,385.00

Exercise 5 — Retirement Age Check

Problem statement:
Write an algorithm that reads a person’s sex and age, then prints whether they are of retirement age, based on:
  • Men: retirement age is greater than 65 years.
  • Women: retirement age is greater than 62 years.
ejercicio5.py
sex = input("Enter sex (M/F): ").strip().upper()
age = int(input("Enter age: "))

if sex == "M":
    if age > 65:
        print("This person IS of retirement age (men retire after 65).")
    else:
        print("This person is NOT yet of retirement age.")
elif sex == "F":
    if age > 62:
        print("This person IS of retirement age (women retire after 62).")
    else:
        print("This person is NOT yet of retirement age.")
else:
    print("Invalid sex entered. Please use M or F.")
Example runs:
Enter sex (M/F): M
Enter age: 70
This person IS of retirement age (men retire after 65).
Enter sex (M/F): F
Enter age: 60
This person is NOT yet of retirement age.
The nested if pattern (an if inside another if) is a natural fit here: the outer conditional selects the correct retirement threshold based on sex, and the inner one compares the actual age against that threshold.

Build docs developers (and LLMs) love