Why a Game Is the Best First Project

Most first projects feel like homework with a different name: a calculator, a to-do list, a form that saves a name. A game is different, because a game has a player — and the player is you. You already know how one feels: the tension of a wrong guess, the satisfaction of narrowing it down.

A guessing game is also honest about what you need to learn. No framework to install, no graphics library to fight, no file to upload. The whole game is three ideas you already know: a loop that repeats, an if statement that compares two numbers, and a counter that stops the game. If you can write those three things, you can write a game.

The Rules, in One Paragraph

The computer picks a secret number between 1 and 100. You guess. After every guess the game tells you one of three things: too low, too high, or correct. You get six guesses. Guess the number in six tries and you win; use them all up and the game tells you the number. Every wrong guess shrinks the range that can still be right.

The smart way to play is to guess the middle of what is left. Start at 50: too high means the answer is below 50, too low means it is above. Each guess halves the possibilities, so a perfect player finds any number from 1 to 100 in at most seven guesses. Six tries rewards that halving strategy and punishes random guessing.

Try It: Win in Four, Lose in Six

Paste it into the browser editor and run it — the guesses are passed in as a list, so the whole game plays itself in one run and you can replay it exactly.

# game.py - your first game: the computer picks a number, you guess it
import random def play(secret, guesses, limit=6): lines = [] for i, guess in enumerate(guesses, 1): if i > limit: lines.append("Out of tries! The number was %d." % secret) return "\n".join(lines) if guess == secret: lines.append("Guess %d: %d - Correct! You won in %d %s." % (i, guess, i, "try" if i == 1 else "tries")) return "\n".join(lines) elif guess < secret: lines.append("Guess %d: %d - Too low." % (i, guess)) else: lines.append("Guess %d: %d - Too high." % (i, guess)) lines.append("Out of tries! The number was %d." % secret) return "\n".join(lines) print(play(42, [50, 25, 37, 42]))
print("---")
print(play(7, [10, 5, 7]))
print("---")
print(play(90, [10, 20, 30, 40, 50, 60, 70]))
print("---")
print(play(42, [42]))
print("---")
print(play(42, []))

The output is five small games. The first wins in four tries: 50 is too high, 25 too low, 37 too low, 42 correct. The second wins in three: 10 too high, 5 too low, 7 correct. The third loses: six guesses from 10 to 60, all too low, and the game stops with "Out of tries! The number was 90." — the seventh guess, 70, never gets checked. The fourth wins on the first guess; the last, an empty list, loses without one.

How the Game Works: Three Ideas, One Function

The first idea is the loop. enumerate(guesses, 1) walks through the guesses and counts them at the same time, so i is the guess number and guess is the number itself. One loop counts and judges together, which is why the transcript reads like a real game.

The second idea is the order of the checks. The win is checked first: if guess == secret, the game stops right there and returns. Only then does it ask whether the guess is too low, and anything left must be too high. Check the win second and a correct guess would be called too low or too high first.

The third idea is the early return. The function does not wait until the loop finishes to decide the game is over: the moment a guess is correct, it returns the transcript, and the moment the guess count passes the limit, it returns the loss. That is why the losing game never checks the seventh guess.

What Breaks, and What It Teaches You

The empty game is the first surprise: play(42, []) prints "Out of tries!" without a single guess. The loop simply never runs, so the last line is the only line. It is correct — a game with no guesses is one you did not play — and it shows why the losing message lives after the loop.

Nothing in play() stops you from guessing 500 when the secret is between 1 and 100 — it just says "Too high." and burns a turn. Real games reject that guess instead of counting it. The fix is one more check at the top of the loop: if the guess is below 1 or above 100, say so and move on without counting it.

The third surprise is the off-by-one hiding in the limit. The check is i > limit, not i >= limit, because i counts guesses that have already started: on the sixth guess i is 6, which is not greater than 6, so the guess is judged. Change it to >= and the sixth guess would never be judged — the game would end after five. Trace the boundary by hand.

Make It Yours: From Fixed Game to Real Game

The version above is fixed on purpose: the secret and the guesses are written into the code, so the same run always gives the same result. That is what makes it testable — and boring after the second run. The first change is one line: replace the secret with random.randint(1, 100) and the game is different every time.

Then change the rules, one at a time, and run after each change. Give the player a score — 100 points minus 10 per guess — and print it on a win. Add a difficulty choice: easy is 1 to 50 with 8 guesses, hard is 1 to 200 with 8 guesses. Let the player type guesses with input() instead of passing a list, now that every path is proven. Finish something small, then change one thing.

From a Snippet to a Portfolio Piece

When your game wins and loses the way you expect, save it as a snippet called "first-guessing-game" with a one-line note: "a number-guessing game in Python — six tries, too high or too low after every guess, random secret." Then push it to GitHub with a short README, following the portfolio guide.

It is also the start of a longer path. The loop in this game — hidden state, a player choice, feedback, a win or a loss — is the same loop in a quiz, a word game, and a text adventure, and the flashcard quiz is the natural next build: same loop, different questions. The Lab courses build the same ideas further.

When the Free Plan Runs Out

Games burn executions fast. Free gives you 20 executions a day, 10 AI requests a day, 10 saved snippets, 3 private snippets, and 3 collaborators per snippet. When you start building a game every day, that is the upgrade moment: Pro is $9/month or $69/year — unlimited saved snippets and executions, 100 AI requests a day, 10 collaborators per snippet, unlimited private snippets, and GitHub integration. Team ($19/user/month, or $15/user/month billed annually) covers a class or study group building games together — see the pricing page.

Your next step is tonight: paste the code above into the browser editor, run it, and watch it win in four tries and lose in six. Then change one thing — a random secret, or the limit from 6 to 4 — run it again, and save it as a snippet called "first-guessing-game". The first time your code plays a game back at you, a program stops being something you write for a grade and becomes something you can play.