Skip to content

Commit 496ae6c

Browse files
authored
Create guessmenumber.py
1 parent bdab885 commit 496ae6c

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

guessmenumber.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import random
2+
3+
def guess_number():
4+
target_number = random.randint(1, 100)
5+
attempts = 0
6+
7+
print("Welcome to the Number Guessing Game!")
8+
print("I've selected a random number between 1 and 100. Can you guess it?")
9+
10+
while True:
11+
guess = int(input("Enter your guess: "))
12+
attempts += 1
13+
14+
if guess < target_number:
15+
print("Too low! Try again.")
16+
elif guess > target_number:
17+
print("Too high! Try again.")
18+
else:
19+
print(f"Congratulations! You guessed the number {target_number} in {attempts} attempts.")
20+
break
21+
22+
if __name__ == "__main__":
23+
guess_number()

0 commit comments

Comments
 (0)