forked from ShadabImran07/python-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
guessing game.py
28 lines (27 loc) · 995 Bytes
/
guessing game.py
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
low = 1
high = 1000
print("Please think of a number between {} and {}".format(low, high))
input("Press ENTER to start")
guesses = 1
while low != high:
#print("\tGuessing in the range of {} to {}".format(low, high))
guess = low + (high - low) // 2
high_low = input("My guess is {}. Should I guess higher or lower? "
"Enter h or l, or c if my guess was correct"
.format(guess)).casefold()
if high_low == "h":
# Guess higher. The low end of the range becomes 1 greater than the guess.
low = guess + 1
elif high_low == "l":
# Guess lower. The high end of the range becomes one less than the guess.
high = guess - 1
elif high_low == "c":
print("I got it in {} guesses!".format(guesses))
break
else:
print("Please enter h, l or c")
break
guesses += 1
else:
print("You thought of the number {}".format(low))
print("I got it in {} guesses".format(guesses))