Making a basic guessing game

Introduction

In this project, we will be making out first game! Throughout the lessons, we will learn about how to make a basic game algorithm as well as how to cross reference the user’s input to the random number set. This will also be the first time we will learn about the random library. Let’s start!

Components Needed

  • Little Python Brain
  • A tablet/phone/laptop
Steps

1

Importing the random library

This is the library responsible for producing and setting random numbers. Down the line, we will also be using it to choose a random element out of a list or tuple we have. For now, simply import it. We’ll get to setting a random number immediately on the next step.

2

Getting a random integer

To set a random integer or number, simply take an x = y block from the Operators category and copy the code shown on the block to the right.

number = random.randint(1, 5)
# Takes a random integer between 1 and 5

This block simply takes a random integer between the numbers that we set (1 and 5 in this case. Simply put, integers are whole numbers, meaning numbers that don’t have a decimal or a fraction.

3

A for loop to repeat 3 times

Next up is a for loop. This for loop will initiate a variable called attempt that starts at 0 and increments by 1 every time it loops. It starts at 0, 1, 2, then stops at 3. This means that we get 3 attempts to guess the number set by the randint() command from the previous step.

4

Getting the user's guess

We then put a User_Input block. What this does is take the user’s guess and stores it in the User_Input variable.

 

5

Converting the user's input to integer

Recall that we are setting a random integer data type. A User_Input function typically returns a string data type. Hence, we now have to convert the User_Input from before to an integer data type to compare it with the right number

6

The main conditional statements

We then want to set 3 conditions to check for.

if userGuess == number:

What this does is simply compare whether the userGuess is equal to the number set by the block on step 2. Note the 2 equal signs! One equal sign is usually used for ASSIGNING DATA to variables while two equal signs is for COMPARING two elements.

elif userGuess > number:

We then compare if the guess is higher than the number.

elif userGuess < number:

Finally, we check if the guess is lower than the number.

7

Specifying the code for each statement

For the first if statement, where the user correctly guesses, we want to do two things: tell the user they’re correct and END the loop. This is done by printing a feedback to say “Correct!” and a break code. A break is used to end while and for loops early.

For the second and third conditions, we just want to tell the user that their guess is “Too high!” or “Too low!”

8

Giving feedback for 3 wrong guesses

Finally, we will add another conditional statement AFTER the for loop.
if userGuess != number: 
  print("The number is: ", number)
What this does is check if the userGuess is not equal ( != ) to the number after 3 attempts then tells them the number with a print statement.

The Complete Code

Try running the code a few times. Have fun! Next up we’ll be learning how to loop this whole chunk of code.

Back to: Intermediate > Guessing Game

CONTACT US

    Log in with your credentials

    or    

    Forgot your details?

    Create Account