Green Hat – Logic & Loops
intermediate30 XP

Logic Projects

Build a number guessing game using everything you know

Logic Projects

Guess the Number!

Python
import random

secret = random.randint(1, 10)
attempts = 0

while attempts < 3:
    guess = int(input("Guess (1-10): "))
    attempts += 1
    if guess == secret:
        print(f"You got it in {attempts} tries!")
        break
    elif guess < secret:
        print("Too low!")
    else:
        print("Too high!")
else:
    print(f"Out of guesses! It was {secret}")
⌨️

Type it yourself

Don't just read it — type the example from above into the box, press Run ▶, and watch what happens. Typing it yourself is how it really sticks! Stuck? Tap Show example.

Python · Playground
Output
Press ▶ Run to see your code come to life…

Quick check

01/2

Which module generates random numbers?