Practical Exercises
From Theory to Practice
Up until now, you’ve learned the building blocks: variables, operators, conditionals, and loops. But theory alone doesn’t make you a hacker. Real mastery comes when you apply concepts in projects - small experiments that combine multiple skills into working programs.
This chapter introduces two classic exercises: a Mini Calculator and a Number Guessing Game. They may seem simple, but they’re powerful training grounds. Each project forces you to weave together input/output, operators, conditionals, and loops into a coherent flow. Think of them as your first “hacks” - programs that interact, decide, and repeat.
Why Exercises Matter
- Integration of Concepts: Projects combine multiple fundamentals into one workflow.
- Problem‑Solving Mindset: You learn to break problems into smaller steps and implement them logically.
- User Interaction: Exercises simulate real‑world scenarios where programs respond to user input.
- Confidence Building: Running a working project gives immediate feedback and motivation.
Mini Calculator
A calculator is the perfect playground for operators and conditionals.
print("Mini Calculator")
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
operator = input("Enter operator (+, -, *, /): ")
if operator == "+":
print("Result:", num1 + num2)
elif operator == "-":
print("Result:", num1 - num2)
elif operator == "*":
print("Result:", num1 * num2)
elif operator == "/":
if num2 != 0:
print("Result:", num1 / num2)
else:
print("Error: Division by zero")
else:
print("Invalid operator")
- Why? This exercise integrates input, operators, and conditionals. It also introduces error handling (division by zero).
- Real‑World Analogy: Just like a pocket calculator, but built by you.
Number Guessing Game
Games are fun, but they also teach loops, conditionals, and randomness.
import random
print("Number Guessing Game")
secret_number = random.randint(1, 10)
attempts = 0
while True:
guess = int(input("Guess a number between 1 and 10: "))
attempts += 1
if guess == secret_number:
print(f"Congratulations! You guessed it in {attempts} attempts.")
break
elif guess < secret_number:
print("Too low, try again.")
else:
print("Too high, try again.")
- Why? This exercise combines loops (
while), conditionals (if/elif/else), and randomness (random.randint). - Real‑World Analogy: Like cracking a safe - you keep trying until you hit the right combination.
The Hacker’s Notebook
- Projects are where theory becomes practice. They reveal how concepts interact in real workflows. The Mini Calculator teaches precision - operators and conditionals working together with user input.
- The Number Guessing Game teaches persistence - loops, randomness, and feedback shaping user experience. Error handling (like division by zero) is critical. Hackers anticipate problems before they occur.
Hacker’s Mindset: treat small projects as experiments. Each one is a lab where you test, fail, refine, and succeed.
