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 a658dc6

Browse files
Add files via upload
1 parent 1ce2c1f commit a658dc6

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

‎numberguessing.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import random
2+
3+
def number_guessing_game(low, high, max_attempts):
4+
number_to_guess = random.randint(low, high)
5+
attempts = 0
6+
7+
print(f"Guess the number between {low} and {high}. You have {max_attempts} attempts.")
8+
9+
while attempts < max_attempts:
10+
attempts += 1
11+
user_guess = input("Enter your guess: ")
12+
13+
try:
14+
user_guess = int(user_guess)
15+
except ValueError:
16+
print("Please enter a valid integer.")
17+
continue
18+
19+
if user_guess == number_to_guess:
20+
print(f"Congratulations! You've guessed the right number in {attempts} attempts.")
21+
break
22+
elif user_guess < number_to_guess:
23+
print("Try again! You guessed too low.")
24+
else:
25+
print("Try again! You guessed too high.")
26+
27+
if attempts == max_attempts:
28+
print(f"Sorry, you've used all your attempts. The number was {number_to_guess}.")
29+
break
30+
31+
# Start the game
32+
number_guessing_game(1, 100, 10)

0 commit comments

Comments
(0)

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