Python-100-Days/Day01-15/code/Day08/guess.py

57 lines
1.2 KiB
Python
Raw Normal View History

2018-04-27 00:00:22 +08:00
"""
面向对象版本的猜数字游戏
Version: 0.1
Author: 骆昊
Date: 2018-03-08
"""
from random import randint
class GuessMachine(object):
2019-05-03 21:17:36 +08:00
def __init__(self):
self._answer = None
self._counter = None
self._hint = None
2018-04-27 00:00:22 +08:00
2019-05-03 21:17:36 +08:00
def reset(self):
self._answer = randint(1, 100)
self._counter = 0
self._hint = None
2018-04-27 00:00:22 +08:00
2019-05-03 21:17:36 +08:00
def guess(self, your_answer):
self._counter += 1
if your_answer > self._answer:
self._hint = '小一点'
elif your_answer < self._answer:
self._hint = '大一点'
else:
self._hint = '恭喜你猜对了'
return True
return False
2018-04-27 00:00:22 +08:00
2019-05-03 21:17:36 +08:00
@property
def counter(self):
return self._counter
2018-04-27 00:00:22 +08:00
2019-05-03 21:17:36 +08:00
@property
def hint(self):
return self._hint
2018-04-27 00:00:22 +08:00
if __name__ == '__main__':
2019-05-03 21:17:36 +08:00
gm = GuessMachine()
play_again = True
while play_again:
game_over = False
gm.reset()
while not game_over:
your_answer = int(input('请输入: '))
game_over = gm.guess(your_answer)
print(gm.hint)
if gm.counter > 7:
print('智商余额不足!')
play_again = input('再玩一次?(yes|no)') == 'yes'