WhatsApp Number: +1(249) 265-0080
Guess the Number Game
Write a program to perform the following functionality.
Part 1 Call a function to display the heading.
Part 2 Write the following loop: Prompt the user for a limit.
Call a function to play the game which will: Calculate a random number using the limit entered by the user.
Display a message showing the range to be guessed.
Write a loop that will: Prompt the user for a number.
Compare the user guess to the generated random number.
If the guess is too high, display message and retry.
If the guess is too low, display message and retry.
If the guess is correct, display message and exit loop.
Check our essay writing services here
Guess the Number Game
This program includes:
display_heading
function: Displays the heading of the game.play_game
function: Handles the game logic by generating a random number within the limit, prompting the user for guesses, and providing feedback until the correct guess is made.main
function: Prompts the user for a limit and starts the game, validating input to ensure the limit is a valid number greater than 1.
Check the python service we offer to understand the python programming language here
import random
def display_heading():
print(“==============================”)
print(” Guess the Number “)
print(“==============================”)
def play_game(limit):
# Generate a random number within the limit
random_number = random.randint(1, limit)
print(f”I have chosen a number between 1 and {limit}. Try to guess it!”)
while True:
try:
# Prompt the user for a guess
guess = int(input(“Enter your guess: “))
# Check if the guess is too high, too low, or correct
if guess > random_number:
print(“Too high! Try again.”)
elif guess < random_number:
print(“Too low! Try again.”)
else:
print(f”Congratulations! You guessed the number {random_number} correctly.”)
break
except ValueError:
print(“Invalid input! Please enter a valid number.”)
def main():
# Part 1: Display the heading
display_heading()
# Part 2: Get the limit from the user and start the game
while True:
try:……