Intuition
Let's build a program that guesses what number you're thinking of simply by correcting the output of the program. The computer will guess some number and show it to you. You will then tell the computer if the number you're thinking of is higher or lower. The computer will guess again and repeat the process. As this process repeats, the computer will slowly become more and more accurate until it guesses the correct number. Now that you understand the intuition behind the program, we'll start programming step by step.
First, the computer will guess a number. We can easily do this by using the built-in random module in Python. We will assume that the number will always be between 0 and 100, and we will keep track of the number of guesses the computer has produced before guessing the correct number. This variable, called tries
, will start at 0.
1import random
2
3guess = random.randint(0, 100)
4tries = 0
Now we will build the skeleton of the program. The computer will keep guessing until it guesses the correct number. We can implement this by creating an infinite loop and then ask the user if the guess is correct. If it is, then we will break out of the loop. We also need code that accounts for if the guess is incorrect. These blocks of code will tell the computer if the correct number is higher or lower.
1import random
2
3guess = random.randint(0, 100)
4tries = 0
5
6# This is known as the training loop in ML since we're updating our parameter "guess" in this loop
7while True:
8 tries += 1
9 print("My guess is: ", guess)
10 print('''Is this guess correct?
11 1: Yes!
12 2: The number is higher
13 3: The number is lower
14
15 :
16 ''', end='')
17 comp = int(input())
18 # Input validation
19 if comp not in [1, 2, 3]:
20 print("Input is invalid")
21 continue
22
23 if comp == 1:
24 print("I did it!")
25 print("Total tries: ", tries)
26 break
27 elif comp == 2:
28 # Guess a higher number
29 elif comp == 3:
30 # Guess a lower number
We only need the logic part of updating the guess
variable to complete the program. We could simply increment/decrement guess
by one, but this would be very tedious. If we implemented this code, then the computer would require many tries. We can improve the program by incrementing the guess by a greater value. Let's call this value jump
(since the guess is jumping towards the answer).
1jump = 20
2
3# to decrement
4guess -= jump
5
6# to increment
7guess += jump
Notice, however, if we keep jump
fixed, then something will go very wrong during the game. Can you see the problem? The guess will never get close to the actual answer! We somehow need to decrement the value of jump throughout the iterations. However, if we only need to jump in a positive direction to get to the answer, then there is no need to decrease the jump length since this will just slow the convergence process.
So what can we do here? The solution is to keep track of the jump direction. If the jump direction changes, then we know that we jumped too far. In this case, we would need to jump back in the other direction at a smaller value. Let's see it in code:
1import random
2
3guess = random.randint(0, 100)
4tries = 0
5jump = 20
6last_comp = 0 # Keeps track of the direction
7decrement_jump = 2
8
9while True:
10 # 80
11 tries += 1
12 print("My guess is: ", guess)
13 print('''
14
15 Is this guess correct?
16 1: Yes!
17 2: The number is higher
18 3: The number is lower
19
20 : ''', end='')
21 comp = int(input())
22 if comp not in [1, 2, 3]:
23 print("Input is invalid")
24 continue
25
26 # Oops, we jumped too far. So decrement jump
27 if last_comp != comp:
28 jump /= decrement_jump
29
30 if comp == 1:
31 print("I did it!")
32 print("Total tries: ", tries)
33 break
34 elif comp == 2:
35 guess += jump
36 elif comp == 3:
37 guess -= jump
38
39 last_comp = comp
The program is now complete! Run it, and the computer should be able to guess your number after a few tries. You can see example output below in which the number was 20:
1My guess is: 99
2Is this guess correct?
3 1: Yes!
4 2: The number is higher
5 3: The number is lower
6
7 : 3
8My guess is: 89.0
9Is this guess correct?
10 1: Yes!
11 2: The number is higher
12 3: The number is lower
13
14 : 3
15My guess is: 79.0
16Is this guess correct?
17 1: Yes!
18 2: The number is higher
19 3: The number is lower
20
21 : 3
22My guess is: 69.0
23Is this guess correct?
24 1: Yes!
25 2: The number is higher
26 3: The number is lower
27
28 : 3
29My guess is: 59.0
30Is this guess correct?
31 1: Yes!
32 2: The number is higher
33 3: The number is lower
34
35 : 3
36My guess is: 49.0
37Is this guess correct?
38 1: Yes!
39 2: The number is higher
40 3: The number is lower
41
42 : 3
43My guess is: 39.0
44Is this guess correct?
45 1: Yes!
46 2: The number is higher
47 3: The number is lower
48
49 : 3
50My guess is: 29.0
51Is this guess correct?
52 1: Yes!
53 2: The number is higher
54 3: The number is lower
55
56 : 3
57My guess is: 19.0
58Is this guess correct?
59 1: Yes!
60 2: The number is higher
61 3: The number is lower
62
63 : 2
64My guess is: 24.0
65Is this guess correct?
66 1: Yes!
67 2: The number is higher
68 3: The number is lower
69
70 : 3
71My guess is: 21.5
72Is this guess correct?
73 1: Yes!
74 2: The number is higher
75 3: The number is lower
76
77 : 3
78My guess is: 22.75
79Is this guess correct?
80 1: Yes!
81 2: The number is higher
82 3: The number is lower
83
84 : 3
85My guess is: 22.125
86Is this guess correct?
87 1: Yes!
88 2: The number is higher
89 3: The number is lower
90
91 : 3
92My guess is: 21.5
93Is this guess correct?
94 1: Yes!
95 2: The number is higher
96 3: The number is lower
97
98 : 3
99My guess is: 20.875
100Is this guess correct?
101 1: Yes!
102 2: The number is higher
103 3: The number is lower
104
105 : 3
106My guess is: 20.25
107Is this guess correct?
108 1: Yes!
109 2: The number is higher
110 3: The number is lower
111
112 : 3
113My guess is: 19.625
114Is this guess correct?
115 1: Yes!
116 2: The number is higher
117 3: The number is lower
118
119 : 2
120My guess is: 19.9375
121Is this guess correct?
122 1: Yes!
123 2: The number is higher
124 3: The number is lower
125
126 : 2
127My guess is: 20.25
128Is this guess correct?
129 1: Yes!
130 2: The number is higher
131 3: The number is lower
132
133 : 3
134My guess is: 20.09375
135Is this guess correct?
136 1: Yes!
137 2: The number is higher
138 3: The number is lower
139
140 : 1
141I did it!
142Total tries: 20