Hands typing code on a laptop
Programming

Lab 2 · Python Basics Lab: Variables, Input, Loops — Number-Guessing Game

Hands-on Lab 275 min readLevel: 🟢 Easy · Beginner100% FreeBy Tahmeer Tech

🎯 Objective

Prove you can use the four Python foundations together in one finished program, not isolated snippets.

🧩 Skills You Will Practice

  • Variables and types
  • input() and int() conversion
  • while loops and conditionals
  • Debugging with print

🧰 What You Need

  • Python 3 installed
  • VS Code or any editor
  • A file game.py
  • Notebook to predict output before running

📋 Step-by-Step Tasks

  1. In game.py store a secret number with the random module: import random, then secret = random.randint(1, 50). Print a welcome message and the rules.
  2. Read the first guess with input() and convert it with int() inside a try/except so letters do not crash the program.
  3. Build a while loop that continues while guess != secret. Inside it, count attempts, and print Too high or Too low using if / elif / else.
  4. When the guess is correct, exit the loop and print the total attempts plus a rating: 5 or fewer Excellent, 9 or fewer Good, otherwise Completed.
  5. Add a replay feature: ask Play again? (y/n), reset secret and attempts if yes, end cleanly if no.
  6. Test deliberately: guess outside 1–50, type a word, and win on the first try. Fix every crash you find and note what caused it.
  7. Add comments at the top: your name, what the program does, and one thing you would improve next.

✅ Expected Result / Deliverable

A runnable game.py that handles wrong types, counts attempts, gives high/low feedback and offers replay — run it three times and keep the terminal output.

⚠️ Common Mistakes

  • Comparing a string from input() directly with an integer
  • Infinite loop because the guess is never re-read inside the loop
  • No attempt counter reset on replay
Safety, Ethics & Pro Tip

Type the code yourself. Using AI to explain an error is learning; pasting a finished game you cannot explain line by line is not completing this lab.