# Coding=utf-8
# Number of guesses
# The process of recording the guessing number
Import Random
Com_result=[] #存放电脑结果, array
Com_count=0 #存放电脑猜测次数
Ran=random.randint (0,100) #随机生成数字
Print (' Start guessing start guessing ')
up=100 #设置上限和下限
Down=0
Print (' Human provide random number is: ', ran) #人随机提供一个数
Guessing=0 #为循环初始化
while (guessing<=0):
Com_number=round ((down+up)/2) #设置每次循环, computer-guessed random number
If Com_number<ran:
Down=com_number #猜小了, raise the lower limit
Com_result.append (Com_number) #把猜测的数字存入数组
Com_count+=1 #猜测次数 +1
Print (Com_number, ' is smaller than ', ran) #显示猜测数字
If Com_number>ran:
Up=com_number
Com_result.append (Com_number)
Com_count+=1
Print (Com_number, ' is bigger than ', ran)
If Com_number==ran:
Com_result.append (Com_number)
Com_count+=1
Print (' Finally ')
Print (' computer\ ' s guessing list: ', Com_result)
Print (' computer has tried: ', com_count)
Guessing=1 #结束猜测
Start Guessinghuman provide random number is: 6750 is smaller than 6775 are bigger than 6762 is Smalle R than 6768 is bigger than 6765 are smaller than 6766 is smaller than 67FinallyComputer ' s guessing list:
[50,----------------------67]computer tried: 7
Python guessing script (computer guessing) (dichotomy)