give me a full code for a game that i can make

1 answer

Sure! Below is an example of a simple text-based "Guess the Number" game written in Python. This is a console application where the player has to guess a randomly generated number within a certain range. The game will give hints to help the player guess the correct number.

To run this code, ensure you have Python installed on your machine. You can then copy and paste the code into a .py file and run it.

import random

def guess_the_number():
    print("Welcome to 'Guess the Number'!")
    print("I have selected a number between 1 and 100.")
    print("Can you guess what it is?")

    # Randomly select a number between 1 and 100
    secret_number = random.randint(1, 100)
    attempts = 0

    while True:
        # Prompt the player for a guess
        guess = input("Enter your guess (or type 'exit' to quit): ")

        if guess.lower() == 'exit':
            print("Thanks for playing! Goodbye!")
            break

        # Validate the player's input
        try:
            guess = int(guess)
            attempts += 1

            # Check the player's guess
            if guess < 1 or guess > 100:
                print("Please guess a number between 1 and 100.")
            elif guess < secret_number:
                print("Too low! Try again.")
            elif guess > secret_number:
                print("Too high! Try again.")
            else:
                print(f"Congratulations! You've guessed the number {secret_number} in {attempts} attempts!")
                break

        except ValueError:
            print("Invalid input. Please enter a number between 1 and 100.")

# Start the game
if __name__ == "__main__":
    guess_the_number()

How to Run the Game

  1. Install Python: Make sure you have Python 3.x installed on your computer. You can download it from python.org.

  2. Copy the Code: Copy the code provided above into a text editor (like Notepad, VS Code, or any other code editor).

  3. Save the File: Save the file with a .py extension, for example, guess_the_number.py.

  4. Open a Terminal/Command Prompt: Navigate to the directory where you saved your file.

  5. Run the Game: Type the following command and hit Enter:

    python guess_the_number.py
    
  6. Play the Game: Follow the on-screen instructions to play the game! You can guess the number or type 'exit' to quit at any time.

Enjoy coding and playing your game! Let me know if you have any questions or need further modifications.