Python Learning Series · @onlysamarrr · Day 15 / 30
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
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.
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")
40 Cannot divide by zero 15 Too low Too high Correct!
Calculators and guessing games combine everything from Phase 1: variables, conditions, loops, lists and functions.
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?
| 71/450 | Concept | Phase 1 recap |
| 72/450 | Live demo | Calculator project |
| 73/450 | Common mistake | Beginner ki top 3 galtiyan |
| 74/450 | Quiz | Quiz: Phase 1 challenge |
| 75/450 | Interview / real life | Number guessing game |