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 numbersprice = 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.
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?
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.
Show solution
ejercicio2.py
price = 12_000discount = 1_000tax_rate = 0.13# Option A: discount first, then taxoption_a = (price - discount) * (1 + tax_rate)# Option B: tax first, then discountoption_b = price * (1 + tax_rate) - discountprint(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.00Option B (tax → discount): ₡12,560.00Option A is cheaper: apply the discount first.
Applying the discount before tax saves ₡130 because the 13% tax is calculated on a smaller base.
Write an algorithm that reads two values, determines the larger one, and prints it with an identification message.
Show solution
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: 45Enter the second value: 72The larger value is B: 72.0
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.
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.
Show solution
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): MEnter age: 70This person IS of retirement age (men retire after 65).
Enter sex (M/F): FEnter age: 60This 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.