Put it all together · About 12 minutes

The secret number machine

The big little idea

Let’s combine input, numbers, and decisions into a tiny guessing game. A program is often a collection of little ideas you already understand.

secret = 7
guess = int(input())
if guess == secret:
    print("You found it!")
elif guess < secret:
    print("Try a bigger number.")
else:
    print("Try a smaller number.")

Use int(input()) to turn an answer into a number. An elif branch checks another condition when the previous one was False. This version gives one guess per run; change the input and run again for a new guess.

Predict, then try: read the example from top to bottom. What do you think it will print? Copy it into the editor below to test your prediction.

Your turn, code explorer.

The secret is 9. Make the program say You found it! when the supplied input is 9. Then experiment with smaller and larger guesses.

You found it!
my_adventure.pyPython · in your browser

Edit the code. Then run it. Ctrl / ⌘ + Enter also works.

OutputYOUR IDEAS, ALIVE
Your program’s output will appear here.

Ready when you are. ✦

The first run loads Python. This can take a moment.

Program input For programs that use input()

Add answers before you run. Each input() reads the next line. Use made-up names and details.

Real Python, powered by Pyodide. Standard-library programs work here; desktop windows, turtle graphics, and package installation are not supported by this editor. Programs stop after 10 seconds of execution.

Back to the starting code?

This replaces your edits for this activity. Download your code first if you want to keep it.

A little hint, please

The comparisons already work. Update secret to 9, then make sure your Program input is 9.

Show one possible solution

There can be more than one way. Read this version, then try writing it yourself.

secret = 9
guess = int(input())
if guess == secret:
    print("You found it!")
elif guess < secret:
    print("Try a bigger number.")
else:
    print("Try a smaller number.")
Pack this idea for later: You can build a real game by connecting small programming ideas.