CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
jackfrued

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: jackfrued/Python-100-Days
Path: blob/master/Day01-15/code/Day08/guess.py
Views: 729
1
"""
2
面向对象版本的猜数字游戏
3
4
Version: 0.1
5
Author: 骆昊
6
Date: 2018-03-08
7
"""
8
9
from random import randint
10
11
12
class GuessMachine(object):
13
14
def __init__(self):
15
self._answer = None
16
self._counter = None
17
self._hint = None
18
19
def reset(self):
20
self._answer = randint(1, 100)
21
self._counter = 0
22
self._hint = None
23
24
def guess(self, your_answer):
25
self._counter += 1
26
if your_answer > self._answer:
27
self._hint = '小一点'
28
elif your_answer < self._answer:
29
self._hint = '大一点'
30
else:
31
self._hint = '恭喜你猜对了'
32
return True
33
return False
34
35
@property
36
def counter(self):
37
return self._counter
38
39
@property
40
def hint(self):
41
return self._hint
42
43
44
if __name__ == '__main__':
45
gm = GuessMachine()
46
play_again = True
47
while play_again:
48
game_over = False
49
gm.reset()
50
while not game_over:
51
your_answer = int(input('请输入: '))
52
game_over = gm.guess(your_answer)
53
print(gm.hint)
54
if gm.counter > 7:
55
print('智商余额不足!')
56
play_again = input('再玩一次?(yes|no)') == 'yes'
57
58