Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 48 additions & 35 deletions guessing-game/guessing-game.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,51 @@

# Introduction
print("Welcome to the Guessing Game!")
print("I'm thinking of a number between 1 and 100.")
print("You have 7 attempts to guess it correctly.")

# Generate a random number between 1 and 100
secret_number = random.randint(1, 100)

# Set the number of allowed attempts
attempts = 7

# Game loop
while attempts > 0:
try:
# Ask the user for their guess
guess = int(input("Enter your guess: "))

# Check if the guess is correct
if guess == secret_number:
print("🎉 Congratulations! You guessed the number!")
break
elif guess < secret_number:
print("Too low. Try a higher number.")
else:
print("Too high. Try a lower number.")

# Decrease remaining attempts
attempts -= 1
print(f"Attempts left: {attempts}\n")

except ValueError:
# Handle non-integer input
print("Invalid input. Please enter a number.\n")

# If no attempts are left
if attempts == 0:
print(f"❌ Game over! The number was {secret_number}. Better luck next time.")

while True:
print("I'm thinking of a number between 1 and 100.")
print("You have 7 attempts to guess it correctly.")

# Generate a random number between 1 and 100
secret_number = random.randint(1, 100)

# Set the number of allowed attempts
attempts = 7

# Game loop
while attempts > 0:
try:
# Ask the user for their guess
guess = int(input("Enter your guess: "))

# Ensure that guess is between 1-100
if guess < 1 or guess > 100:
print("Please enter a number between 1 and 100.\n")
continue

# Check if the guess is correct
if guess == secret_number:
print("🎉 Congratulations! You guessed the number!")
break
elif guess < secret_number:
print("Too low. Try a higher number.")
else:
print("Too high. Try a lower number.")

# Decrease remaining attempts
attempts -= 1
print(f"Attempts left: {attempts}\n")

except ValueError:
# Handle non-integer input
print("Invalid input. Please enter a number.\n")

# If no attempts are left
if attempts == 0:
print(f"❌ Game over! The number was {secret_number}. Better luck next time.")

# Play again option
play_again = input("Do you want to play again? (y/n): ").strip().lower()
if play_again != "y":
print("Thanks for playing!")
break