- 2nd Jan 2024
- 21:38 pm
import random
def guess_the_number():
print("Welcome to the Guess the Number 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:
user_guess = int(input("Enter your guess (between 1 and 100): "))
attempts += 1
if user_guess < 1 or user_guess > 100:
print("Please enter a number between 1 and 100.")
continue
if user_guess < secret_number:
print("Too low! Try a higher number.")
elif user_guess > secret_number:
print("Too high! Try a lower number.")
else:
print(f"Congratulations! You've guessed the number {secret_number} in {attempts} attempts!")
break
except ValueError:
print("Invalid input! Please enter a valid number.")
# Start the game
guess_the_number()
This game generates a random number between 1 and 100, and the player has to guess the number. After each guess, the program provides hints ('Too low!' or 'Too high!') based on the comparison with the secret number. The game continues until the player guesses the correct number.
About The Author - Raj Patel
Raj Patel, a proficient Python developer and coding enthusiast, presents the "Guess the Number Game in Python Programming." With a knack for creating engaging and interactive projects, Raj's expertise shines through in this simple yet entertaining game. Through clear explanations and practical examples, Raj empowers readers to delve into Python programming, fostering both learning and enjoyment in the world of coding.