File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change
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 ()
You can’t perform that action at this time.
0 commit comments