-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgass_num1.py
More file actions
70 lines (59 loc) · 1.46 KB
/
gass_num1.py
File metadata and controls
70 lines (59 loc) · 1.46 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#! /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内,判断需要猜测几次
"""
import math
def gassNum(top=100):
target = 0
tmp = 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 = {
'tmp': math.floor(top/2),
'top': top,
'target': target,
'count': 1,
'gassList': [math.floor(top/2)]
}
gass(**result)
def gass(**args):
print(args)
tmp=args['tmp']
target = args['target']
top = args['top']
if args['tmp'] == args['target']:
print("complete")
return args
else:
args['count'] += 1
if tmp > target:
tmp = math.floor((tmp - 1)/2)
else:
tmp = math.floor((tmp + top)/2)
args['tmp'] = tmp
judgeExist(**args)
gass(**args)
def judgeExist(**args):
tmp = args['tmp']
gassList = args['gassList']
top = args['top']
if tmp in gassList:
for index in range(tmp,top):
if index not in gassList:
tmp = index
args['tmp'] = tmp
else:
args['gassList'].append(tmp)
gassNum()