How to Make a Python Random Number Guessing Game

Ryan Barone
January 23, 2024

In this Python challenge, let's make a "guess the number" game!

Using variables, data types, user input, conditional statements, and loops, the program will generate a random number and then ask the user to guess what the number is. When the user guesses wrong, the program will tell them if they are too high or too low until the correct number is guessed. 

Here are some guidelines:

  • Generate a random number between 1 and 100.
  • Let the user guess what the number is until they get it right.
  • Give the user feedback after each guess (tell them if their guess is too high, too low, or just right).
  • Begin the game again.

To get started, create a new Python File called guessing_game. On line 1, write a comment to describe what you'll be working on. 

Tip: You should add comments throughout your program. It helps to first break the problem down into steps. Then, add comments first to structure out where the code goes then add in the code!

add comments python.jpeg

Here is what your final code should look like:

 

# The following program generates a guessing game.
import random

computer_number = random.randrange(0, 100)

guessed = False

while True:
  if not guessed:
      answer = input("Guess the number: ")
      if int(answer) == computer_number:
          guessed = True
          print("You got it!")
          break
      elif int(answer) > computer_number:
          print("Your guess is too high.")
      else:
          print("Your guess is too low.")
  else:
      break

Let's break down each of the main Python concepts as they appear in the code above:

"computer_number": This variable stores the number generated by the computer that the user has to guess. It's assigned a random number between 0 and 99 (inclusive) using the random.randrange(0, 100) function.

"guessed": A boolean variable set to "False" initially. It's used to track whether the user has guessed the correct number.

Integer: The "computer_number" and the user's guess (converted using "int(answer)") are integers.
Boolean: The "guessed" variable is a boolean, holding either "True" or "False".
String: The user's input ("answer") is initially a string before being converted to an integer.

The "input" function is used to capture the user's guess. The line "answer = input("Guess the number: ")" displays a prompt and waits for the user to enter a guess. The input is initially treated as a string.

"if not guessed:" This checks whether the user has not yet guessed the number correctly. If guessed is False, the program continues to ask for user input.

"if int(answer) == computer_number:" This checks if the user's guess equals the computer's number. If true, guessed is set to True, and a congratulatory message is printed.

"elif int(answer) > computer_number:" This executes if the user's guess is higher than the computer's number, printing a hint that the guess is too high.

"else:" This is the final condition, executing if none of the above conditions are met, indicating the guess is too low.

The "while True:" loop is an infinite loop that keeps the game running until the correct number is guessed. The loop continues to execute as long as "True" is provided, which is always the case here.

The "break" statement is used to exit the loop when the correct number is "guessed" or when guessed becomes "True".

Moving Forward

To enhance the code, you can add additional features to your game to make it more challenging and fun! Here are some examples:

Use a variable to track how many guesses the user has made already. When users win, tell them how many tries it took.

Or, let users set the upper bound or choose a difficulty mode (1 to 1,000 instead of 1 to 100!)

Last, create a function for code that you use a lot such as getting user input or generating a random number.

Need help or want to dive into Python a bit more? Check out our in-person Python summer camps or virtually with our online Python tutoring

Meet iD Tech!

Sign up for our emails to learn more about why iD Tech is #1 in STEM education! Be the first to hear about new courses, locations, programs, and partnerships–plus receive exclusive promotions! AI summer camps, coding classes for kids, and more!

By signing up you agree to our Privacy policy
Subscribe & Save!

Meet iD Tech!

Sign up for our emails to learn more about why iD Tech is #1 in STEM education! Be the first to hear about new courses, locations, programs, and partnerships–plus receive exclusive promotions! AI summer camps, coding classes for kids, and more!

By signing up you agree to our Privacy policy

Meet iD Tech!

Sign up for our emails to learn more about why iD Tech is #1 in STEM education! Be the first to hear about new courses, locations, programs, and partnerships–plus receive exclusive promotions! AI summer camps, coding classes for kids, and more!

By signing up you agree to our Privacy policy
Subscribe & Save!

Meet iD Tech!

Sign up for our emails to learn more about why iD Tech is #1 in STEM education! Be the first to hear about new courses, locations, programs, and partnerships–plus receive exclusive promotions! AI summer camps, coding classes for kids, and more!

By signing up you agree to our Privacy policy