Skip to content

Instantly share code, notes, and snippets.

@HandsonMatheus
Created March 12, 2025 00:30
Show Gist options
  • Save HandsonMatheus/48a412808036a41cdc0c03b9c7812f43 to your computer and use it in GitHub Desktop.
Save HandsonMatheus/48a412808036a41cdc0c03b9c7812f43 to your computer and use it in GitHub Desktop.
import random
def play_guessing_game():
# GENERATE A RANDOM NUMBER BETWEEN 1 AND 100
secret_number = random.randint(a=1, b=100)
attempts = 0
guessed = False
print("\nwelcome to the Handson's Number Guessing Game")
print("I am thinking of a number between 1 and 100.")
while not guessed:
try:
# GET USER'S GUESS
guess = int(input("\nEnter your guess >>> "))
attempts += 1
# CHECK IF GUESS IS CORRECT
if guess == secret_number:
print(f"Congratulations! You guessed the number in {attempts} attempts!")
guessed = True
elif guess < secret_number:
print("Too low! Try again.")
else:
print("Too high! Try again.")
except ValueError:
print("Please enter a secret number")
return attempts
def main():
play_again = "y"
while play_again.lower() == "y":
attempts = play_guessing_game()
play_again = input("\nDo you want to play again? (y/n) >>> ")
print("\nThanks for playing! Goodbye!")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment