top of page

25% Discount For All Pricing Plans "welcome"

Number Guessing Game


What is the Number Guessing Game?

The Number Guessing Game is a simple game where a player tries to guess a randomly selected number within a certain range. After each guess, the player is told whether the guess is correct, or if the number is higher or lower than the selected number. The player continues to guess until they find the correct number.


Project Goals and Student Benefits

With this project, students will reinforce their ability to develop simple and functional programs using basic Python knowledge. Additionally, they will gain experience in user interaction and random number generation.


Project Outcomes

  • Creating a game simulation using basic Python structures and control flows.

  • Receiving data from the user and managing the game flow by processing this data.

  • Writing more modular and readable code with the help of functions.


Step-by-Step Tasks of the Project

  1. Selecting a random number.

  2. Receiving a guess from the user.

  3. Checking the user's guess and providing feedback.

  4. Continuing the game until the user makes the correct guess.

  5. Displaying the result of the game on the screen.

  6. Making the code more modular using functions.


Step 1: Selecting a Random Number

As the first step, a random number will be selected within a certain range.

import random

# Selecting a random number
random_number = random.randint(1, 100)

This code snippet selects a random number between 1 and 100 and assigns it to the random_number variable. The random.randint() function returns a random integer within the specified range.


Step 2: Receiving a Guess from the User

In the second step, a guess will be taken from the user.

# Receiving a guess from the user
def get_guess():
    """Receives a guess from the user."""
    guess = int(input("Guess a number between 1 and 100: "))
    return guess

This function receives and returns a guess from the user.


Step 3: Checking the User's Guess and Providing Feedback

In the third step, the user's guess will be checked and feedback will be provided.

# Checking the user's guess and providing feedback
def check_guess(guess, random_number):
    """Checks the user's guess and provides feedback."""
    if guess < random_number:
        print("Guess a higher number.")
        return False
    elif guess > random_number:
        print("Guess a lower number.")
        return False
    else:
        print("Congratulations! Correct guess.")
        return True

This function checks the user's guess and provides feedback. If the guess is correct, it returns True; otherwise, it returns False.


Step 4: Continuing the Game until the User Makes the Correct Guess

In the fourth step, the game will continue until the user makes the correct guess.

# Continuing the game until the user makes the correct guess
def continue_game(random_number):
    """Continues the game until the user makes the correct guess."""
    correct_guess = False
    while not correct_guess:
        guess = get_guess()
        correct_guess = check_guess(guess, random_number)

This function continues the game until the user makes the correct guess.


Step 5: Displaying the Result of the Game on the Screen

In the fifth step, the result of the game will be displayed on the screen.

# Displaying the result of the game on the screen
def game_end():
    """Displays the result of the game on the screen."""
    print("The game has ended. Would you like to play again? (y/n)")
    decision = input().lower()
    return decision == 'y'

This function displays the result of the game on the screen and asks the user if they would like to play again.


Step 6: Solution with Functions


Starting the Game Function

This function starts the Number Guessing Game and manages the game flow.

def start_game():
    """Starts the Number Guessing Game."""
    while True:
        random_number = random.randint(1, 100)
        print("A number between 1 and 100 has been selected.")
        continue_game(random_number)
        if not game_end():
            break

# Starting the game
start_game()

This function starts the Number Guessing Game and manages the game flow. It receives guesses from the user, checks the guesses, and determines the result of the game.


Topics Used

  • Data Types: In this project, integers (random number, user guess) and strings (user input) are used.

  • Conditional Statements: if-else structures are used to check the user's guess and provide feedback.

  • Functions: Functions ensure that the code is modular and reusable.

  • Loops: The while loop is used to continue the game until the user makes the correct guess.

  • Random Module: The random.randint() function is used to select a random integer within the specified range.

Comentarios


bottom of page