Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jun 29, 2024. It is now read-only.

Commit 63187c1

Browse files
number_guessing
1 parent b492236 commit 63187c1

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

‎thattururajitha/number_guessing‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,46 @@ import random
1111
def play_guessing_game():
1212
# Generate a random number between 1 and 50
1313
secret_number = random.randint(1, 50)
14+
# Set the maximum number of attempts to 10
1415
max_attempts = 10
16+
# Initialize the number of attempts made by the player
1517
attempts = 0
1618

19+
# Welcome message for the player
1720
print("Welcome to the Number Guessing Game!")
18-
print(f"I'm thinking of a number between 1 and 100. Can you guess it?")
21+
# Informing the player about the range of the secret number
22+
print(f"I'm thinking of a number between 1 and 50. Can you guess it?")
1923

24+
# Loop until the player runs out of attempts or guesses the number
2025
while attempts < max_attempts:
2126
try:
27+
# Prompt the player to enter their guess
2228
guess = int(input("Enter your guess: "))
29+
# Increment the number of attempts
2330
attempts += 1
2431

32+
# Check if the player's guess is correct
2533
if guess == secret_number:
34+
# Congratulate the player and break the loop if the guess is correct
2635
print(f"Congratulations! You guessed it right in {attempts} attempts.")
2736
break
37+
# Provide a hint if the guess is lower than the secret number
2838
elif guess < secret_number:
2939
print("Try a higher number.")
40+
# Provide a hint if the guess is higher than the secret number
3041
else:
3142
print("Try a lower number.")
3243

44+
# Handle non-integer inputs and prompt the player to enter a valid integer
3345
except ValueError:
3446
print("Invalid input. Please enter a valid integer.")
3547

48+
# Inform the player if they have used all their attempts without guessing correctly
3649
if attempts == max_attempts:
3750
print(f"Sorry, you've reached the maximum number of attempts. The secret number was {secret_number}.")
3851

52+
# Check if the script is run directly (not imported)
3953
if __name__ == "__main__":
54+
# Start the game if the script is run directly
4055
play_guessing_game()
56+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /