Build a Guessing Number Game



Tweet

Getting Started

Today we’ll be creating a guessing number game in Python that you can run in your terminal. Open up your favorite text editor and let’s begin!

To start, we’ll need to import random as the random module will let us generate a random number for the user to guess.

Then, we’ll set three variables:

The number variable will be set to random.randint(1,50). This assigns the variable to a random integer between 1 and 50. You may change the range if you wish.

Here’s what our code should look like so far:

import random

numberofGuesses = 0
number = random.randint(1,50)

name = raw_input("Hello! What is your name? ")

Creating the while & if loops

Now, let’s print a message to the user asking to guess a number between 1 and 50. After that, we can run more raw_inputs as guesses and calculate whether the guess was too high or low in a while loop with if statements.

Our while loop will run the functions in it until a certain number of guesses. In this case the while loop will be set to while numberofGuesses < 8:. In the while loop, we can do the following:

That may seem like a lot - and it is!

import random

numberofGuesses = 0
number = random.randint(1,50)

name = raw_input("Hello! What is your name? ")

print(name + ", I am thinking of a whole number between 1 and 50. Can you guess what it is?")

while numberofGuesses < 8:
  guess = raw_input("Take a guess ")
  guess = int(guess)

  numberofGuesses = numberofGuesses + 1;
  guessesLeft = 8 - numberofGuesses;

  if guess < number:
    guessesLeft=str(guessesLeft)
    print("Your guess is too low! You have " + guessesLeft + " guesses left")

  if guess > number:
    guessesLeft=str(guessesLeft)
    print("Your guess is too high! You have " + guessesLeft + " guesses left")

  if guess==number:
    break

if guess==number:
  numberofGuesses=str(numberofGuesses)
  print("Good job! You guessed the number in " + numberofGuesses + " tries :)")

if guess!=number:
  number=str(number)
  print("Sorry. The number I was thinking of was " + number + " :)")

Take note that when we want to either use the number in code our print it out, we need to convert it. First, we convert it to an integer (int(guess)). In the same way, when we want to print out a number, we make sure it is converted to a string (str(number)).