2023-07-26 17:25:58 +00:00
|
|
|
import random
|
2023-07-26 17:14:59 +00:00
|
|
|
|
2023-07-26 17:25:58 +00:00
|
|
|
def guess_the_number():
|
|
|
|
secret_number = random.randint(1, 100)
|
|
|
|
attempts = 0
|
2023-07-26 17:14:59 +00:00
|
|
|
|
2023-07-26 17:25:58 +00:00
|
|
|
print("Welcome to Guess the Number Game!")
|
|
|
|
print("I'm thinking of a number between 1 and 100.")
|
|
|
|
print("Can you guess it?")
|
2023-07-26 17:14:59 +00:00
|
|
|
|
2023-07-26 17:25:58 +00:00
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
guess = int(input("Enter your guess: "))
|
|
|
|
attempts += 1
|
2023-07-26 17:22:22 +00:00
|
|
|
|
2023-07-26 17:25:58 +00:00
|
|
|
if guess == secret_number:
|
|
|
|
print(f"Congratulations! You guessed the number {secret_number} in {attempts} attempts.")
|
|
|
|
break
|
|
|
|
elif guess < secret_number:
|
|
|
|
print("Try a higher number.")
|
|
|
|
else:
|
|
|
|
print("Try a lower number.")
|
|
|
|
except ValueError:
|
|
|
|
print("Invalid input. Please enter a valid number.")
|
2023-07-26 17:14:59 +00:00
|
|
|
|
2023-07-26 17:25:58 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
guess_the_number()
|