-
Notifications
You must be signed in to change notification settings - Fork 5
/
binarySearchGame.py
49 lines (46 loc) · 1.23 KB
/
binarySearchGame.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from random import randint
from math import log
#Choose minimum number
while True:
min=raw_input("Choose a minimum number (1 is recommended): ")
try:
min=int(min)
break
except:
print "Please choose a number."
#Choose maximum number
while True:
max=raw_input("Choose a maximum number: ")
try:
max=int(max)
break
except:
print "Please choose a number."
#Generate turn and random number
number=randint(min,max)
turn=int((log(max-min)/log(2))+1)
#print number
#Proceed to the Game
while True:
guess=raw_input("The computer has chosen a number between %s and %s inclusive. Guess the number! You have %s chance(s) left: " % (min,max,turn))
try:
guess=int(guess)
if min<=guess<=max:
if number==guess:
print "Congrats!! You win!"
break
elif turn==1:
print "Sorry. You Lose.."
break
elif number>guess:
print "Try Higher!"
elif number<guess:
print "Try Lower!"
turn-=1
#if guess is out of range
elif min>guess or guess>max:
print "Please enter a number between %s and %s inclusive." % (min,max)
#if raw_input is not an int
except ValueError:
print "Please enter a number between %s and %s inclusive." % (min,max)
raw_input("Press enter to exit. ")