概念有很多,这里就不详细介绍了,提供一个连接:https://blog.csdn.net/Mac_timmy/article/details/75712146
import random
def guess():
count=1
realNumber = random.randint(1, 200)
while True:
guessNumber = int(input("请输入:"))
if guessNumber<realNumber:
count+=1
print("太小了!")
elif guessNumber>realNumber:
print("太大了!")
count += 1
else:
print("你猜对了噢!一共猜了",count,"次!")
break
#二分查找
def binary_search(list,item):
low = 0
high = len(list)-1
while low <= high:
mid = int((low + high)/2)
guess = list[mid]
if guess > item:
high = mid -1
elif guess < item:
low = mid + 1
else:
return mid
return None
if __name__=="__main__":
#guess()
list=[1,2,3,4,5,6,7,8,9,10]
print(binary_search(list,7))