Skip to content

This Python code implements a classic number guessing game where you try to guess a secret number hidden within a user-specified range.

Notifications You must be signed in to change notification settings

TheToriqul/number-guessing-game

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 

Repository files navigation

Number Guessing Game

This Python code implements a classic number guessing game where you try to guess a secret number hidden within a user-specified range. The code provides helpful feedback to guide you towards the correct answer, making it an engaging and fun experience.

Key Features:

  • Dynamic Range: You dictate the upper limit of the guessing range, ensuring flexibility and personalization.
  • Valid Input Handling: The code gracefully handles invalid inputs, such as non-numeric input or numbers outside the designated range, guiding you towards valid input formats.
  • Clear Instructions: Prompts and messages are well-structured and easy to understand, enhancing the usability of the game.
  • Informative Feedback: After each guess, the code indicates whether your guess was too high, too low, or spot-on, offering valuable clues to hone your strategy.
  • Success Notification: Upon guessing the correct number, the code congratulates you and displays the number of guesses it took, providing a sense of accomplishment.

How to Play:

  1. Launch the program: Run the Python script.
  2. Set the maximum range: Enter a positive integer as the highest number you're willing to guess.
  3. Make your guesses: After receiving feedback on each guess, keep refining your attempts until you find the secret number.

Example:

Imagine the maximum range is set to 100. The secret number is 42. Here's a possible gameplay scenario:

Make a guess: 75
You were above the number!
Make a guess: 15
You were below the number!
Make a guess: 38
You were below the number!
Make a guess: 42
You got the correct number :)
You got it in 4 guesses. Congratulation!

I hope you enjoy this number guessing game!

Explaining the Number Guessing Game Codes:

This Python code creates a number guessing game where you try to find a secret number hidden within a range you decide. Let's break it down step-by-step:

1. Imports and Welcome Message:

import random

print("Welcome to the number guessing game!")
  • import random: This line brings in the random module, which allows us to generate random numbers.
  • print("Welcome to the number guessing game!"): This simply prints a welcome message to the user.

2. Setting the Maximum Range:

max_range = input("Type a maximum number of your guess: ")

if max_range.isdigit():
  max_range = int(max_range)
  if max_range <= 0:
    print("Please type a number less than 0 next time")
    quit()
else:
  print("Please type a nice number next time")
  quit()
  • max_range = input(...): This line asks the user to enter the highest number they want to guess (the maximum range).
  • if max_range.isdigit(): This condition checks if the user entered a valid number (digits only).
  • max_range = int(max_range): If it's valid, converts the input string to an integer.
  • if max_range <= 0: If the number is 0 or negative, it's invalid, so the program exits with an error message.
  • else: If the input isn't a number, it's also invalid, and the program exits with an error message.

3. Generating the Secret Number:

random_number = random.randint(0, max_range)
  • random_number = random.randint(0, max_range): This line uses the random module to generate a random integer between 0 and the maximum range you entered. This is the secret number you're trying to guess.

4. Guessing Loop:

guess_score = 0
while True:
  guess_score += 1
  user_guess = input("Make a guess: ")
  if user_guess.isdigit():
    user_guess = int(user_guess)
  else:
    print("Please type a number next time!")
    continue
  if user_guess == random_number:
    print("You got the correct number :)")
    break
  elif user_guess > random_number:
    print("You were above the number!")
  else:
    print("You were below the number!")
  • guess_score = 0: This initializes a variable to keep track of your guesses.
  • while True: This creates a loop that keeps running until you guess the correct number.
  • guess_score += 1: Inside the loop, it increments the guess counter after each attempt.
  • user_guess = input(...): Similar to before, it asks you to enter your guess.
  • if user_guess.isdigit(): Checks if your guess is a valid number.
  • if user_guess == random_number: If your guess matches the secret number, the loop breaks, and you win!
  • elif user_guess > random_number: If your guess is too high, it gives you a hint.
  • else: If your guess is too low, it gives you another hint.

5. Congratulations and Summary:

print("You got it in", guess_score, "guesses. Congratulation!")
  • This line prints a congratulatory message mentioning how many guesses it took you to win.

About

This Python code implements a classic number guessing game where you try to guess a secret number hidden within a user-specified range.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages