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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# Asks the user if we found the number or if we have to guess again
def evaluate(number):
	
	print "Is your number %d? (Yes: 'Y', Higher: 'H', Lower: 'L')" % number
	
	valid_answers = 'Y', 'H', 'L'
	
	while True:	
		answer = raw_input("> ")
		
		if answer.upper() in valid_answers:
			return answer.upper()
		else:
			print "Invalid input, try again..."

# Explain the situation
print """Hello, pick a number so we can play a game of me trying to guess it.
Don't you dare cheating pal, I'll cut you! Now let's play!
When you are ready press ENTER
"""
raw_input()

# Set the bounds of the guess
max_number = max_guess = 100
min_number = min_guess = 1

# Counters
tries = 0
wins = 0

while True:
	
	# Calculate the computer's guess
	# We try to guess in the middle of our current guess-range
	guess = (min_guess + max_guess) / 2
	tries += 1
	
	# Check the guess with the player
	result = evaluate(guess);
	
	# Did we get it?
	if result == 'Y':
		
		# Boast a little about our small success and ask for another round
		print """
		Hurray, I rule! I had to try %d times.
		So far I have won %d games.
		Wanna play again? (Y/N)
		""" % (tries, wins)
		wins += 1
		
		play_again = raw_input("> ")
		
		
		# If we are to play again we must initialize some stuff
		if play_again.upper() == 'Y':
			max_guess = 100
			min_guess = 1
			tries = 0
		# Otherwise, we just break out of the loop
		else:
			break
			
	elif result == 'H':
		# Got to guess higher than what we did
		min_guess = guess
	else:
		# Got to guess lower than what we did
		max_guess = guess