File tree 1 file changed +34
-0
lines changed
1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change
1
+ def main ():
2
+ print ('think of a number between 0 and 100' )
3
+ min_value = 0
4
+ max_value = 100
5
+
6
+ while True :
7
+ query = min_value + (max_value - min_value ) // 2
8
+ if check_is_answer (query ):
9
+ # you know you have the right answer
10
+ break
11
+ if check_is_greater (query ):
12
+ # you know the number must be greater than query
13
+ min_value = query + 1
14
+ else :
15
+ # you know the number must be less than query
16
+ max_value = query - 1
17
+ print ('Your number was ' , query )
18
+
19
+
20
+ def check_is_answer (value ):
21
+ return ask_true_false ('Is your number ' + str (value )+ '?' )
22
+
23
+
24
+ def check_is_greater (value ):
25
+ return ask_true_false ('Is your number greater than ' + str (value )+ '?' )
26
+
27
+
28
+ def ask_true_false (prompt ):
29
+ response = input (prompt + ' (y/n) ' )
30
+ return response == 'y' or response == 'Y'
31
+
32
+
33
+ if __name__ == "__main__" :
34
+ main ()
You can’t perform that action at this time.
0 commit comments