Higher-Lower Game with Python

SarahDev
2 min readSep 2, 2023

I happy to help you create a Higher-Lower game tutorial in Python! The Higher-Lower game is a simple guessing game where the player tries to guess a randomly chosen number within a specified range. The game provides hints whether the next guess should be higher or lower than the previous one.

Here’s a step-by-step tutorial on how to create the Higher-Lower game:

import random

def higher_lower_game():
# Set the range for the random number
lower_bound = 1
upper_bound = 100
secret_number = random.randint(lower_bound, upper_bound)

attempts = 0
guess = None

print(f"Welcome to the Higher-Lower game! Guess a number between {lower_bound} and {upper_bound}.")

while guess != secret_number:
try:
guess = int(input("Enter your guess: "))
except ValueError:
print("Please enter a valid number.")
continue

attempts += 1

if guess < secret_number:
print("Higher!")
elif guess > secret_number:
print("Lower!")

print(f"Congratulations! You guessed the number {secret_number} in {attempts} attempts.")

if __name__ == "__main__":
higher_lower_game()

Copy and paste the above code into a Python file (e.g., higher_lower_game.py), and then run the file using your preferred Python interpreter.

--

--