Python Learning Series · @onlysamarrr · Day 15 / 30

Day 15: Mini Project + Recap

Day 15 is the Phase 1 mini project and recap: a calculator built with a function and a dictionary of operations, and a number guessing game built with a while-style loop and conditions.

Phase: Beginner · Reels: 71/450 – 75/450

Quick Answer

Day 15 is the Phase 1 mini project and recap: a calculator built with a function and a dictionary of operations, and a number guessing game built with a while-style loop and conditions.

What You Will Learn

Code

def calc(a, op, b):
    if op == "/" and b == 0:
        return "Cannot divide by zero"
    ops = {"+": lambda: a + b, "-": lambda: a - b, "*": lambda: a * b, "/": lambda: a / b}
    return ops[op]() if op in ops else "Invalid op"

print(calc(8, "*", 5))
print(calc(8, "/", 0))
print(calc(10, "+", 5))
secret = 7
guesses = [3, 9, 7]
for guess in guesses:
    if guess == secret:
        print("Correct!")
        break
    print("Too high" if guess > secret else "Too low")

Output

40
Cannot divide by zero
15
Too low
Too high
Correct!

Code Explained, Line by Line

  1. calc(a, op, b) takes two numbers and an operator and returns the result.
  2. Division by zero is checked first, so the program returns a message instead of crashing.
  3. A dictionary maps each operator to its job, using a small lambda function for each.
  4. ops[op]() picks the right operation from the dictionary and runs it; an unknown operator gives 'Invalid op'.
  5. The guessing game compares each guess with the secret and breaks out of the loop on a correct guess.
  6. Top 3 beginner mistakes: = vs ==, forgetting int() on input(), and wrong indentation.
Common Mistake: The three most common beginner errors are dividing by zero, confusing = with ==, and using input() without int().

Real-Life Use

Calculators and guessing games combine everything from Phase 1: variables, conditions, loops, lists and functions.

Practice Task: Phase 1 challenge: add -, / and % to the calculator, and add a counter that counts tries in the guessing game.

Test Yourself — MCQs

Select (tick) an option, then tap Check answer to see instantly whether your answer is right or wrong, with an explanation. You can change your answer and check again.

Q1. What is calc(8, "*", 5)?

Q2. Why check b == 0 before division?

Q3. In the guessing game with secret 7 and guesses [3, 9, 7], what prints first?

Today's 5 Reels (from the 90-Day Plan Excel)

71/450ConceptPhase 1 recap
72/450Live demoCalculator project
73/450Common mistakeBeginner ki top 3 galtiyan
74/450QuizQuiz: Phase 1 challenge
75/450Interview / real lifeNumber guessing game
← Day 14Day 16 →