Python Loops
💡
Exercise 18

Guess Number 15 XP Medium

Ctrl+Enter Run Ctrl+S Save

Let's combine loops with random numbers! We'll create a number guessing game.

The break keyword can exit a loop early:

while True: print('This runs once') break # exits the loop

And the += operator is shorthand for addition:

count = 0 count += 1 # same as count = count + 1
📋 Instructions
Create a number guessing game simulation: 1. Import `random` 2. Generate a secret number between 1-10 3. Use a while loop to simulate guesses 4. Print the secret number and how many attempts it took Since this uses randomness, any valid output is accepted!
Use a while loop. Generate random number with random.randint(). Compare guess to number. Give hints like 'Too high!' or 'Too low!'. Count the attempts!
⚠️ Try solving it yourself first — you'll learn more!
while True:
    print('This runs once')
    break  # exits the loop
main.py
Hi! I'm Rex 👋
Output
Ready. Press ▶ Run or Ctrl+Enter.