-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgass_num.py
More file actions
55 lines (42 loc) · 1.01 KB
/
gass_num.py
File metadata and controls
55 lines (42 loc) · 1.01 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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2020 wmsj100 <wmsj100@hotmail.com>
#
# Distributed under terms of the MIT license.
"""
猜数字100内,判断需要猜测几次
"""
def gassNum(top=100):
target = 0
count = 0 # 猜测次数
while True:
target = input("Please input number(1-100)")
if not target.isdigit():
print("Please input correct number(1-100)")
else:
target = int(target)
break
result = {
'lower': 0,
'upper': top,
'middle': 0,
'target': target,
'count': 0,
}
gass(**result)
def gass(**args):
middle = args['middle'] = int((args['lower'] + args['upper'])/2)
target = args['target']
args['count'] += 1
print(args)
if middle == target:
return args
else:
if middle > target:
args['upper'] = middle-1
else:
args['lower'] = middle+1
gass(**args)
gassNum(100)