Skip to content

Commit 31aa1dc

Browse files
authored
Guess_a_number.py
1 parent 08b7bdd commit 31aa1dc

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

AI_Guess_a_number/Guess_a_number.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
import random
3+
4+
def play_game():
5+
print("Welcome to the Number Guessing Game!")
6+
print("Think of a number between 1 and 100, and I will try to guess it.")
7+
print("Press 'y' if my guess is correct, 'l' if your number is lower, or 'h' if your number is higher.")
8+
9+
low = 1
10+
high = 100
11+
guess_count = 0
12+
correct_guess = False
13+
14+
while not correct_guess:
15+
guess = random.randint(low, high)
16+
print("Is your number", guess, "?")
17+
user_input = input("Enter 'y', 'l', or 'h': ")
18+
19+
if user_input == "y":
20+
print("I guessed it! Your number is", guess)
21+
print("I made", guess_count, "guesses.")
22+
correct_guess = True
23+
elif user_input == "l":
24+
high = guess - 1
25+
guess_count += 1
26+
elif user_input == "h":
27+
low = guess + 1
28+
guess_count += 1
29+
else:
30+
print("Invalid input. Please try again.")
31+
32+
play_game()

0 commit comments

Comments
 (0)