#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'huhu, <huyoo353@126测试数据>'
def find_middle(start, end):
#print start, end
return round((start+end)/2.0)
if __name__ == '__main__':
start, end = '',''
text = raw_input(u"> 输入猜数的范围(如:421-499 或者421 499 或者421,499):").decode('gb18030')
spliters = '-, '
for c in spliters:
if text.find(c) != -1:
num_list = text.split(c)
if ''.join(num_list).isdigit():
start, end = num_list[0],num_list[1]
break
if start == '' or end == '':
print u'范围不正确'
else:
start = int(start)
end = int(end)
count = 1
last_guess = find_middle(start,end)
while 1:
result = raw_input(u"放弃猜测直接回车, 等于输入=, 小了输入1, 大了请输入2\n>>> #猜数[%d] ,对吗?> " % last_guess ).decode('gb18030')
#print type(text)
if result in ['q','e','exit','quit','bye',u'退出']:
print 'Bye!'
break
else:
result=result.strip()
if result == '1':
start = last_guess
last_guess = find_middle(last_guess,end)
elif result == '2':
end = last_guess
last_guess = find_middle(start,last_guess)
elif result == '=':
print u'恭喜猜中, 共猜了%d次' % count
print u'#猜数[%d]' % last_guess
break
else: #
continue
count += 1 以上就是Python实现破解猜数游戏实例详解的详细内容,更多请关注Gxl网其它相关文章!
查看更多关于Python实现破解猜数游戏实例详解的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did84428