DEV Community

Cover image for Title: Guess Master – The Ultimate Number Guessing Challenge
Sarthak Gholve
Sarthak Gholve

Posted on

Title: Guess Master – The Ultimate Number Guessing Challenge

🎯 Guess Master is a fun and interactive Python number-guessing game where the computer secretly chooses a number between 1 and 100. Try to find the hidden number using hints like β€œToo High!” or β€œToo Low!”. Your attempts are counted, and you can quit anytime. Can you find the number in the fewest guesses? 🐍πŸ”₯
here's the code:

import random

def start_game():

secret_number = random.randint(1, 100)
attempts = 0

print("Welcome to the Number Guessing Game!")
print("I am thinking of a number between 1 and 100.")

while True:

user_input = input("Take a guess (or type 'quit' to exit): ").strip()


if user_input.lower() == 'quit':
    print(f"Thanks for playing! The number was {secret_number}.")
    break


if not user_input.isdigit():
    print("Please enter a valid whole number.")
    continue


guess = int(user_input)
attempts += 1


if guess < secret_number:
    print("Too low! Try again.")
elif guess > secret_number:
    print("Too high! Try again.")
else:
    print(f"πŸŽ‰ Correct! You found the number in {attempts} attempts!")
    break
Enter fullscreen mode Exit fullscreen mode
Enter fullscreen mode Exit fullscreen mode




Run the game

if name == "main":
start_game()

Top comments (1)

Collapse
 
sarthakgholve13-py profile image
Sarthak Gholve

a good number guesser game try it out