How to Create a Higher Lower Game in Python

Posted by

Create Higher Lower Game in Python

How to Create a Higher Lower Game in Python

In this tutorial, we will learn how to create a simple Higher Lower game in Python. The game will ask the player to guess a number between 1 and 100, and will respond with whether the player’s guess is higher or lower than the correct number. Let’s get started!

Step 1: Import the random module

First, we need to import the random module in order to generate a random number for the player to guess. This can be done with the following code:


import random

Step 2: Generate a random number

We will generate a random number between 1 and 100 using the randint() function from the random module. This can be done with the following code:


number = random.randint(1, 100)

Step 3: Ask the player for their guess

Next, we will ask the player to guess a number between 1 and 100. We will use the input() function to get the player’s guess, and convert it to an integer using the int() function. This can be done with the following code:


guess = int(input("Guess a number between 1 and 100: "))

Step 4: Check if the player’s guess is higher or lower

We will compare the player’s guess to the random number that we generated earlier, and respond with whether the player’s guess is higher or lower than the correct number. This can be done with the following code:


if guess number:
print("Lower")
else:
print("Congratulations! You guessed the correct number.")

Step 5: Repeat the process until the player guesses correctly

We will put steps 3 and 4 inside a while loop to allow the player to keep guessing until they guess the correct number. This can be done with the following code:


while guess != number:
guess = int(input("Guess again: "))

if guess number:
print("Lower")
else:
print("Congratulations! You guessed the correct number.")

Step 6: Run the program

Finally, we will run the program and play the Higher Lower game by executing the Python script. The full code for the game should look like this:

import random

number = random.randint(1, 100)

guess = int(input("Guess a number between 1 and 100: "))

while guess != number:
    if guess  number:
        print("Lower")
    
    guess = int(input("Guess again: "))
    
print("Congratulations! You guessed the correct number.")

And that’s it! You have now created a Higher Lower game in Python. Have fun playing!

0 0 votes
Article Rating
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@qaquest
1 month ago

Subscribe for more Python videos 🐍