Number Guessing Game in Python

·

3 min read

Table of contents

Imagine a scenario where you, as the player, are challenged to guess a secret number hidden within a specified range. The computer holds the key to this elusive number, and your task is to crack the code through a series of educated guesses. This interactive and beginner-friendly project will not only introduce you to fundamental Python concepts but also provide a hands-on experience in building a fully functional game.

Let's embark on this journey together as we unravel the magic behind Python programming and turn lines of code into an entertaining guessing game.

Structure of the code

  1. import random: This line imports the random module, which provides functions for generating random numbers. In this case, it's used to generate a random secret number for the player to guess.

  2. def guessing_game():: This line defines a function named guessing_game. Functions in Python are blocks of reusable code. In this case, the function contains the logic for the guessing game.

  3. print("Welcome to the Guessing Game!"): This line prints a welcome message to the console when the game starts.

  4. Setting up the game parameters:

    • lower_limit = 1: Defines the lower limit of the range for the random number.

    • upper_limit = 100: Defines the upper limit of the range for the random number.

    • secret_number = random.randint(lower_limit, upper_limit): Generates a random number between the lower and upper limits and assigns it to secret_number.

  5. attempts = 0 and max_attempts = 10: Initializes a variable attempts to 0, representing the number of attempts the player has made, and max_attempts is set to 10, determining the maximum number of attempts allowed.

  6. while attempts < max_attempts:: This line starts a while loop that continues as long as the number of attempts is less than the maximum allowed attempts.

  7. Inside the while loop:

    • guess = int(input(f"Guess the number between {lower_limit} and {upper_limit}: ")): Prompts the player to input a guess and converts the input to an integer.

    • attempts += 1: Increments the number of attempts after each guess.

    • The next block of code checks if the player's guess is correct or not:

      • if guess == secret_number:: If the guess is correct, it prints a congratulatory message, including the secret number and the number of attempts, and then breaks out of the loop.

      • elif guess < secret_number:: If the guess is too low, it prompts the player to try again with a "Too low!" message.

      • else:: If the guess is too high, it prompts the player to try again with a "Too high!" message.

  8. else:: This block is executed if the while loop completes without encountering a break statement. In this case, it means the player has run out of attempts. It prints a message revealing the correct number.

  9. # Run the game: Finally, the last line of code calls the guessing_game() function, initiating the execution of the entire game.

The game presents a formidable challenge, offering a difficulty level that makes achieving success nearly insurmountable. Participants must accurately deduce a number from the extensive range of 1 to 100. Concluding our discussion for today, in the forthcoming article, we will undertake a comprehensive review of Python functions. An intriguing alternative method of coding in Python will be explored and compared against the conventional approach. Your anticipation for this insightful comparison is sincerely appreciated. Please stay tuned for a rewarding continuation of our exploration.