Learn Without Walls
← Back to Module 4

Module 4 Practice Problems

15 problems to sharpen your conditionals skills

Problem 1: Positive, Negative, or Zero

Easy

Write a program that asks the user for a number and prints whether it is positive, negative, or zero.

Example: If the user enters -5, print -5 is negative

Use if, elif, and else. Remember to convert the input to a number with int() or float().

number = float(input("Enter a number: "))

if number > 0:
    print(f"{number} is positive")
elif number < 0:
    print(f"{number} is negative")
else:
    print("The number is zero")

Problem 2: Voting Eligibility

Easy

Ask for the user's age. If they are 18 or older, print that they can vote. Otherwise, tell them how many years until they can vote.

Calculate years remaining with 18 - age.

age = int(input("Enter your age: "))

if age >= 18:
    print("You are eligible to vote!")
else:
    years_left = 18 - age
    print(f"You can vote in {years_left} year(s).")

Problem 3: Grade Calculator

Easy

Ask for a test score (0-100). Print the letter grade: A (90-100), B (80-89), C (70-79), D (60-69), F (below 60).

Use an if-elif-else chain. Check from highest to lowest.

score = int(input("Enter your score (0-100): "))

if score >= 90:
    print("Grade: A")
elif score >= 80:
    print("Grade: B")
elif score >= 70:
    print("Grade: C")
elif score >= 60:
    print("Grade: D")
else:
    print("Grade: F")

Problem 4: Even or Odd

Easy

Ask for a number and print whether it is even or odd.

Use the modulo operator %. A number is even if number % 2 == 0.

number = int(input("Enter a number: "))

if number % 2 == 0:
    print(f"{number} is even")
else:
    print(f"{number} is odd")

Problem 5: Largest of Three

Easy

Ask for three numbers and print the largest one.

Compare the first two, then compare the winner with the third. Or use nested if-elif-else.

a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
c = float(input("Enter third number: "))

if a >= b and a >= c:
    print(f"The largest is {a}")
elif b >= a and b >= c:
    print(f"The largest is {b}")
else:
    print(f"The largest is {c}")

Problem 6: Leap Year Checker

Medium

Ask for a year and determine if it is a leap year. A year is a leap year if it is divisible by 4, but not by 100, unless it is also divisible by 400.

Use and and or: (year % 4 == 0) and (year % 100 != 0 or year % 400 == 0)

year = int(input("Enter a year: "))

if (year % 4 == 0) and (year % 100 != 0 or year % 400 == 0):
    print(f"{year} is a leap year")
else:
    print(f"{year} is not a leap year")

Problem 7: Temperature Converter

Medium

Ask the user for a temperature and whether it is in Celsius or Fahrenheit (C/F). Convert it to the other unit. Also print a message if the temperature is below freezing (0C/32F).

F = C * 9/5 + 32 and C = (F - 32) * 5/9. Use .upper() on the unit input.

temp = float(input("Enter temperature: "))
unit = input("Celsius or Fahrenheit? (C/F): ").upper()

if unit == "C":
    converted = temp * 9/5 + 32
    print(f"{temp}C = {converted:.1f}F")
    if temp < 0:
        print("That's below freezing!")
elif unit == "F":
    converted = (temp - 32) * 5/9
    print(f"{temp}F = {converted:.1f}C")
    if temp < 32:
        print("That's below freezing!")
else:
    print("Invalid unit. Please enter C or F.")

Problem 8: Calculator

Medium

Build a simple calculator. Ask for two numbers and an operation (+, -, *, /). Perform the calculation and print the result. Handle division by zero.

Use if-elif-else to check the operator. For division, check if the second number is zero before dividing.

num1 = float(input("Enter first number: "))
op = input("Enter operation (+, -, *, /): ")
num2 = float(input("Enter second number: "))

if op == "+":
    result = num1 + num2
    print(f"{num1} + {num2} = {result}")
elif op == "-":
    result = num1 - num2
    print(f"{num1} - {num2} = {result}")
elif op == "*":
    result = num1 * num2
    print(f"{num1} * {num2} = {result}")
elif op == "/":
    if num2 == 0:
        print("Error: Cannot divide by zero!")
    else:
        result = num1 / num2
        print(f"{num1} / {num2} = {result}")
else:
    print("Invalid operation.")

Problem 9: BMI Calculator

Medium

Ask for weight (kg) and height (m). Calculate BMI (weight / height^2) and print the category: Underweight (<18.5), Normal (18.5-24.9), Overweight (25-29.9), Obese (30+).

BMI formula: weight / (height ** 2). Use if-elif-else to categorize.

weight = float(input("Enter weight in kg: "))
height = float(input("Enter height in meters: "))

bmi = weight / (height ** 2)

if bmi < 18.5:
    category = "Underweight"
elif bmi < 25:
    category = "Normal weight"
elif bmi < 30:
    category = "Overweight"
else:
    category = "Obese"

print(f"BMI: {bmi:.1f} - {category}")

Problem 10: Day of the Week

Medium

Ask for a number 1-7 and print the corresponding day of the week (1=Monday). If the number is 6 or 7, also print "It's the weekend!" Otherwise print "It's a weekday."

Use if-elif-else for the day name, then a separate if-else for weekend/weekday.

day_num = int(input("Enter a number (1-7): "))

if day_num == 1:
    day = "Monday"
elif day_num == 2:
    day = "Tuesday"
elif day_num == 3:
    day = "Wednesday"
elif day_num == 4:
    day = "Thursday"
elif day_num == 5:
    day = "Friday"
elif day_num == 6:
    day = "Saturday"
elif day_num == 7:
    day = "Sunday"
else:
    day = "Invalid"

if day != "Invalid":
    print(f"Day {day_num} is {day}")
    if day_num >= 6:
        print("It's the weekend!")
    else:
        print("It's a weekday.")
else:
    print("Please enter a number between 1 and 7.")

Problem 11: Password Strength Checker

Medium

Ask for a password and check its strength. Weak: less than 6 characters. Medium: 6-11 characters. Strong: 12+ characters. Also check if it contains at least one digit.

Use len() for length. Use a variable like has_digit and check each character with .isdigit() or use any(c.isdigit() for c in password).

password = input("Enter a password: ")
length = len(password)

# Check length
if length < 6:
    strength = "Weak"
elif length < 12:
    strength = "Medium"
else:
    strength = "Strong"

# Check for digits
has_digit = False
for char in password:
    if char.isdigit():
        has_digit = True

print(f"Password strength: {strength}")
if not has_digit:
    print("Tip: Add a number to make it stronger!")

Problem 12: Movie Ticket Pricing

Hard

Calculate movie ticket price. Base price: $12. Child (under 12): 50% off. Senior (65+): 30% off. Student (with ID): 20% off. Matinee (before 5pm): additional $3 off. 3D movie: additional $3 charge.

Start with the base price, apply the age/student discount first, then matinee discount, then 3D surcharge.

age = int(input("Enter age: "))
is_student = input("Student with ID? (yes/no): ").lower() == "yes"
show_time = int(input("Show time (hour, 0-23): "))
is_3d = input("3D movie? (yes/no): ").lower() == "yes"

price = 12.00

if age < 12:
    price *= 0.50
elif age >= 65:
    price *= 0.70
elif is_student:
    price *= 0.80

if show_time < 17:
    price -= 3

if is_3d:
    price += 3

if price < 0:
    price = 0

print(f"Ticket price: ${price:.2f}")

Problem 13: Rock, Paper, Scissors

Hard

Create a rock-paper-scissors game. The computer always picks "rock" (for now). Ask the user for their choice and determine the winner.

Use nested conditions: first check for a tie, then check each winning combination.

computer = "rock"
player = input("Choose rock, paper, or scissors: ").lower()

if player not in ["rock", "paper", "scissors"]:
    print("Invalid choice!")
elif player == computer:
    print(f"Both chose {player}. It's a tie!")
elif (player == "rock" and computer == "scissors") or \
     (player == "paper" and computer == "rock") or \
     (player == "scissors" and computer == "paper"):
    print(f"You chose {player}, computer chose {computer}. You win!")
else:
    print(f"You chose {player}, computer chose {computer}. You lose!")

Problem 14: Tax Calculator

Hard

Calculate income tax using brackets. First $10,000: 0%. $10,001-$40,000: 10%. $40,001-$85,000: 20%. Over $85,000: 30%. Print the tax owed.

This is a progressive tax. Each bracket applies only to the income within that range, not total income. Calculate tax for each bracket separately.

income = float(input("Enter annual income: $"))
tax = 0

if income <= 10000:
    tax = 0
elif income <= 40000:
    tax = (income - 10000) * 0.10
elif income <= 85000:
    tax = 30000 * 0.10 + (income - 40000) * 0.20
else:
    tax = 30000 * 0.10 + 45000 * 0.20 + (income - 85000) * 0.30

print(f"Income: ${income:,.2f}")
print(f"Tax owed: ${tax:,.2f}")

Problem 15: Adventure Game

Hard

Create a mini text adventure. The player starts at a fork in the road and can go left or right. Each path leads to another choice. Use nested conditionals to create at least 4 possible endings.

Use nested if-else blocks. Each choice narrows down the path. Think of it as a binary tree of decisions.

print("You are at a fork in the road.")
path = input("Go left or right? ").lower()

if path == "left":
    print("You find a river.")
    action = input("Swim across or follow the bank? (swim/follow) ").lower()
    if action == "swim":
        print("You swim across and find a treasure chest! You win!")
    else:
        print("You follow the bank and find a cozy village. Safe ending!")
elif path == "right":
    print("You enter a dark forest.")
    action = input("Take the torch or walk in the dark? (torch/dark) ").lower()
    if action == "torch":
        print("The torch reveals a hidden path to a castle! You win!")
    else:
        print("You trip over a root and fall asleep. Game over!")
else:
    print("You stand still too long and a bird takes your hat. Game over!")