Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- def guess_number():
- secret_number = random.randint(1, 100)
- attempts = 0
- print("Welcome to the Number Guessing Game!")
- print("I'm thinking of a number between 1 and 100.")
- print("You have 10 attempts to guess the number.")
- while attempts < 10:
- try:
- guess = int(input("Take a guess: "))
- except ValueError:
- print("Invalid input. Please enter a valid integer.")
- continue
- attempts += 1
- if guess < secret_number:
- print("Too low! Try again.")
- elif guess > secret_number:
- print("Too high! Try again.")
- else:
- print(f"Congratulations! You guessed the number in {attempts} attempts!")
- return
- print(f"Game over! The number was {secret_number}. Better luck next time!")
- guess_number()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement