Skip to content

Commit cbb3a1c

Browse files
committed
feat: add number guessing game implementation
1 parent bfd7138 commit cbb3a1c

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

number_guessing.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import random
2+
3+
def guessing_game():
4+
print("Welcome to the Number Guessing Game!")
5+
6+
upper_bound = int(input("Write the upper bound of the range: "))
7+
8+
print(f"I'm thinking of a number between 1 and {upper_bound}.")
9+
10+
number_to_guess = random.randint(1, upper_bound)
11+
attempts = 0
12+
guessed_correctly = False
13+
14+
while not guessed_correctly:
15+
try:
16+
guess = int(input("Make a guess: "))
17+
attempts += 1
18+
19+
if guess < number_to_guess:
20+
print("Too low.")
21+
elif guess > number_to_guess:
22+
print("Too high.")
23+
else:
24+
guessed_correctly = True
25+
print(f"Congratulations! You've guessed the number {number_to_guess} in {attempts} attempts.")
26+
except ValueError:
27+
print("Please enter a valid integer.")
28+
29+
if __name__ == "__main__":
30+
guessing_game()

0 commit comments

Comments
 (0)