-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Expand file tree
/
Copy pathnumber_guessing.py
More file actions
30 lines (23 loc) · 911 Bytes
/
Copy pathnumber_guessing.py
File metadata and controls
30 lines (23 loc) · 911 Bytes
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
import random
def guessing_game():
print("Welcome to the Number Guessing Game!")
upper_bound = int(input("Write the upper bound of the range: "))
print(f"I'm thinking of a number between 1 and {upper_bound}.")
number_to_guess = random.randint(1, upper_bound)
attempts = 0
guessed_correctly = False
while not guessed_correctly:
try:
guess = int(input("Make a guess: "))
attempts += 1
if guess < number_to_guess:
print("Too low.")
elif guess > number_to_guess:
print("Too high.")
else:
guessed_correctly = True
print(f"Congratulations! You've guessed the number {number_to_guess} in {attempts} attempts.")
except ValueError:
print("Please enter a valid integer.")
if __name__ == "__main__":
guessing_game()