Python guessing script (computer guessing) (dichotomy)

Source: Internet
Author: User

# 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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.