Number Guessing Game
Number Guessing Game is a very simple and fun game. The player tries to enjoy and input the randomly generated number with a range between 1-100. This game provides you with a guessing game, telling them if the guessing number is correct, too high, or too low. The main goal is to guess the number within their attempts.
There are some Keypoints of the Number Guessing Game
1. Random Number Generation:
- The game produced a random number a defined range ( 1-100)
- It contains a secret number that the player has to guess.
2. Player Input :
- The player is prompted to enter their guess (number).
- The game ensures that the input number is valid or not.
3. Feedback Mechanism
After guessing, the game provides certain feedback
- The player is told to guess higher if the guessing is very low or too low.
- The player is told to guess lower if the guessing number is very high or too high.
- If the guessing number is correct, the player wins the game.
4. Winning Conditions :
- The game ends when the player guesses the correct number.
- The game displays the number of attempts made by users
5. Optional Features :
Difficulty Levels: The range of numbers can be adjusted. For example, 1-00 is easy, 1-500 is medium, and 1-1000 is hard.
Limited Attempts: The player can be provided a limited number of attempts to guess the number.
Hints: The game may provide hints, such as the number is even or odd.
Score System: The Play can earn points on the performance of guessing numbers
How the Game Works (step by step) :
1. The game starts with the generation of a random number, for example, 1-100.
2. The player is prompted to enter their guessing number.
3. The game compares the player’s guessing number to the secret number.
- If the guessing number is lower than the secret number, the game tells the player to guess a higher number.
- If the guess number is higher than the secret number, the game tells the player to guess a lower number. If the guess is correct, then the game congratulates the user and ends the game.
The game can be repeated, the process until the player guesses the correct number
Why is it Fun ?
Challenge: Players enjoy the challenge of narrowing down the correct number using logic and deduction.
Feedback: Immediate feedback after each guessing number keeps the player engaged.
Replayability: Since the secret number is random, the game can be played multiple times, not to be repeated.
Educational Value
Problem-Solving: Players will be educated to use logical reasoning to narrow down the possible range of numbers.
Math Skills: Players can develop his/ her math skills by practicing comparing numbers and understanding ranges.
Programming Practice: For developers, this game is a great way to clear basic programming concepts such as loops, conditionals, and random number generation.
Python Implementation :
Python Code
import random
def number_guessing_game():
# Step 1: Generate a random number between 1 and 100
secret_number = random.randint(1, 100)
attempts = 0
guessed_correctly = False
print("Welcome to the Number Guessing Game!")
print("I have selected a number between 1 and 100. Can you guess what it is?")
# Step 2: Loop until the player guesses the correct number
while not guessed_correctly:
try:
# Step 3: Get the player's guess
guess = int(input("Enter your guess: "))
attempts += 1
# Step 4: Provide feedback based on the guess
if guess < secret_number:
print("Too low! Try again.")
elif guess > secret_number:
print("Too high! Try again.")
else:
guessed_correctly = True
print(f"Congratulations! You've guessed the number in {attempts} attempts.")
except ValueError:
# Handle invalid input (e.g., non-numeric input)
print("Invalid input. Please enter a valid number.")
# Run the game
if __name__ == "__main__":
number_guessing_game()
Code Output
Welcome to the Number Guessing Game!
I have selected a number between 1 and 100. Can you guess what it is?
Enter your guess: 50
Too high! Try again.
Enter your guess: 25
Too low! Try again.
Enter your guess: 37
Too low! Try again.
Enter your guess: 42
Congratulations! You've guessed the number in 4 attempts.