本帖最后由 happy886rr 于 2017-7-2 15:08 编辑
回复 11# codegay
在你的requests.Session()基础上改的。(外链效果图)
- import requests
-
- # 用户信息
- userNAMZE='Psaa'
-
- # 主机地址
- hostURL='http://bbaass.tk/math/'
-
- # 提交数据
- postDATA={'send':'Answer', 'username':userNAMZE, 'math':50}
-
- # 注册用户名
- requests.Session().post(hostURL, {'send':'reg', 'username':userNAMZE})
-
- # 辗转移位
- def GuessFun(mathMIN, mathMAX):
- # 搜索次数
- searchTIMES=0
- while mathMAX-mathMIN >1:
- postDATA['math'] = (mathMAX+mathMIN)>>1
- searchTIMES +=1
- print("The %d times"%(searchTIMES))
- r = requests.Session().post(hostURL, postDATA).json()
- ret = r['re']
- if ret == '=':
- break
- elif ret == '<':
- mathMIN=postDATA['math']
- else:
- mathMAX=postDATA['math']
- return searchTIMES
-
- # 打印结果
- if __name__ == "__main__":
- searchTIMES = GuessFun(-1, 101)
- print("Search times: %d\nGuess the number: %d"%(searchTIMES, postDATA['math']))
复制代码
|