Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- def number_guessing_game():
- print("Welcome to the Number Guessing Game!")
- print("I'm thinking of a number between 1 and 100. Can you guess it?")
- # Generate a random number between 1 and 100
- secret_number = random.randint(1, 100)
- attempts = 0
- while True:
- try:
- guess = int(input("Enter your guess (between 1 and 100): "))
- attempts += 1
- if guess < secret_number:
- print("Too low! Try a higher number.")
- elif guess > secret_number:
- print("Too high! Try a lower number.")
- else:
- print(f"Congratulations! You guessed the number {secret_number} in {attempts} attempts!")
- break # Exit the loop when the correct number is guessed
- except ValueError:
- print("Invalid input! Please enter a valid number.")
- if __name__ == "__main__":
- number_guessing_game()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement