Skip to content

Commit ee2a859

Browse files
authored
Create Exercise-25-Guessing-Game-Two.py
1 parent ce1e7cb commit ee2a859

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

Exercise-25-Guessing-Game-Two.py

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
'''
2+
Exercise 25: Guessing Game Two
3+
4+
In a previous exercise, we’ve written a program that “knows”
5+
a number and asks a user to guess it.
6+
7+
This time, we’re going to do exactly the opposite. You, the
8+
user, will have in your head a number between 0 and 100. The
9+
program will guess a number, and you, the user, will say
10+
whether it is too high, too low, or your number.
11+
12+
At the end of this exchange, your program should print out how
13+
many guesses it took to get your number.
14+
15+
'''
16+
# Solution
17+
def main():
18+
import random
19+
guess_log = [random.randint(0, 101)]
20+
log_dict = {'lower':[0], 'higher':[100]}
21+
while True:
22+
print(guess_log[-1])
23+
evaluate = input('Tell me my number is:\n1: too low\n2: too high\n3: Right\n')
24+
try:
25+
evaluate = int(evaluate)
26+
if evaluate == 1:
27+
log_dict['lower'].append(guess_log[-1])
28+
guess_log.append(int((min(log_dict['higher']) + max(log_dict['lower']))/ 2 ))
29+
elif evaluate == 2:
30+
log_dict['higher'].append(guess_log[-1])
31+
guess_log.append(int((min(log_dict['higher']) + max(log_dict['lower']))/ 2 ))
32+
else:
33+
print('Yeah!\n', guess_log)
34+
break
35+
except ValueError:
36+
print('Input error.')
37+
38+
39+
if __name__ == "__main__":
40+
main()
41+
42+
# Test Part
43+
# >>> %Run test.py
44+
# 68
45+
# Tell me my number is:
46+
# 1: too low
47+
# 2: too high
48+
# 3: Right2
49+
# 34
50+
# Tell me my number is:
51+
# 1: too low
52+
# 2: too high
53+
# 3: Right3
54+
# Yeah!
55+
# [68, 34]
56+
# >>> %Run test.py
57+
# 40
58+
# Tell me my number is:
59+
# 1: too low
60+
# 2: too high
61+
# 3: Right
62+
# 1
63+
# 70
64+
# Tell me my number is:
65+
# 1: too low
66+
# 2: too high
67+
# 3: Right
68+
# 1
69+
# 85
70+
# Tell me my number is:
71+
# 1: too low
72+
# 2: too high
73+
# 3: Right
74+
# 2
75+
# 77
76+
# Tell me my number is:
77+
# 1: too low
78+
# 2: too high
79+
# 3: Right
80+
# 1
81+
# 81
82+
# Tell me my number is:
83+
# 1: too low
84+
# 2: too high
85+
# 3: Right
86+
# 2
87+
# 79
88+
# Tell me my number is:
89+
# 1: too low
90+
# 2: too high
91+
# 3: Right
92+
# 2
93+
# 78
94+
# Tell me my number is:
95+
# 1: too low
96+
# 2: too high
97+
# 3: Right
98+
# 3
99+
# Yeah!
100+
# [40, 70, 85, 77, 81, 79, 78]

0 commit comments

Comments
 (0)