forked from Nagasathvik/Python-Programming-Internship
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumberguessing.py
More file actions
32 lines (25 loc) · 1.04 KB
/
Copy pathnumberguessing.py
File metadata and controls
32 lines (25 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import random
def number_guessing_game(low, high, max_attempts):
number_to_guess = random.randint(low, high)
attempts = 0
print(f"Guess the number between {low} and {high}. You have {max_attempts} attempts.")
while attempts < max_attempts:
attempts += 1
user_guess = input("Enter your guess: ")
try:
user_guess = int(user_guess)
except ValueError:
print("Please enter a valid integer.")
continue
if user_guess == number_to_guess:
print(f"Congratulations! You've guessed the right number in {attempts} attempts.")
break
elif user_guess < number_to_guess:
print("Try again! You guessed too low.")
else:
print("Try again! You guessed too high.")
if attempts == max_attempts:
print(f"Sorry, you've used all your attempts. The number was {number_to_guess}.")
break
# Start the game
number_guessing_game(1, 100, 10)