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.
Guide On Rating System
Vote
Here is a Python program that performs the functionality you described:
```python
import random
def display_heading():
print("Welcome to the Guessing Game!")
def play_game(limit):
random_number = random.randint(1, limit)
print(f"Guess a number between 1 and {limit}")
while True:
user_guess = int(input("Enter your guess: "))
if user_guess > random_number:
print("Too high! Try again.")
elif user_guess < random_number:
print("Too low! Try again.")
else:
print("Congratulations! You guessed correctly.")
break
def main():
display_heading()
limit = int(input("Enter a limit: "))
play_game(limit)
if __name__ == "__main__":
main()
```
This program first displays a heading, then prompts the user for a limit. It then generates a random number within the range specified by the user and prompts the user to guess the number. If the user's guess is too high or too low, it informs the user and prompts for another guess. If the user's guess is correct, it congratulates the user and exits the loop.