Iteration: While Loops


Medium Challenge: Guess the Number Game

Try it yourself!


  • Write a Python program that picks a random number between 1 and 100.
  • The user has to guess the number.
  • The program will tell the user if the guess is too high, too low, or correct.
  • The game continues until the user guesses the correct number.

Example Output:

> Guess the number: 50
Too high

> Guess the number: 25
Too low

> Guess the number: 37
Correct! The number was 37

Loading Python runtime...
  • Use a while loop to repeatedly ask the user for a guess until they get it right.
  • You can use random.randint(1, 100) to generate the random number.
  • Don't forget to import random.
  • Here's a snippet to help: