jaysaadana19 / pythongame

1 stars 0 forks source link

Make Python Game Code #1

Open jaysaadana19 opened 1 year ago

jaysaadana19 commented 1 year ago

import random

def guess_the_number():

Generate a random number between 1 and 100

secret_number = random.randint(1, 100)

# Initialize variables
attempts = 0
guessed = False

print("Welcome to Guess the Number!")
print("I'm thinking of a number between 1 and 100. Can you guess it?")

while not guessed:
    try:
        # Get the player's guess
        guess = int(input("Enter your guess: "))

        # Increment the number of attempts
        attempts += 1

        # Check if the guess is correct
        if guess == secret_number:
            print(f"Congratulations! You guessed the number {secret_number} in {attempts} attempts.")
            guessed = True
        elif guess < secret_number:
            print("Too low! Try again.")
        else:
            print("Too high! Try again.")
    except ValueError:
        print("Invalid input. Please enter a valid number.")

if name == "main": guess_the_number()