-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMind_game.py
More file actions
60 lines (53 loc) · 2.07 KB
/
Copy pathMind_game.py
File metadata and controls
60 lines (53 loc) · 2.07 KB
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
class Mind_game:
def permission(self, prompt, yes_values={"y", "yes"}, no_values={"n", "no"}):
while True:
response = input(prompt).strip().lower()
if response in yes_values:
return True
elif response in no_values:
return False
def get_int(self, prompt, start=None, end=None):
while True:
try:
value = int(input(prompt))
if (start is None or start <= value) and (end is None or value <= end):
return Value
except ValueError:
pass
def get_one_of(self, prompt, values):
while True:
value = input(prompt).strip().lower()
if value in values:
return value
def main(self):
if obj.permission(
"Hey! Please guess a number between 1 and 100. I will try to guess what you have guessed. Press 'Y' to play 'N' to exit"):
start, end = 1, 100
got_it = False
for i in range(1, 8):
mid = (start + end) // 2
print("I guess {}!".format(mid))
temp = obj.get_one_of("Is that correct? Write 'Y' for Yes or 'N' or No", {"y", "n"})
if temp == "y":
got_it = True
print("You Guessed {} !".format(mid))
break
elif temp == "n":
temp1 = obj.get_one_of(
"Is the number greater or lesser than {}? Type G for greater and L for Lesser.".format(mid),
{"g", "l"})
if temp1 == "g":
start = mid + 1
else: # temp1 == "l":
end = mid - 1
if start > end:
break
if got_it:
print("Guessed it in {} guesses!".format(i))
else:
print("Sry Try again")
else:
print("Bye!")
if __name__ == "__main__":
obj = Mind_game()
obj.main()