Home > Backend Development > Python Tutorial > Detailed explanation of examples of cracking guessing games using Python

Detailed explanation of examples of cracking guessing games using Python

巴扎黑
Release: 2017-09-26 10:38:00
Original
4006 people have browsed it

This article mainly introduces the algorithm for cracking the guessing game in Python, briefly describes the principle of the guessing game, and analyzes the relevant implementation techniques of Python to crack the guessing game in the form of specific examples. Friends in need can refer to the following

The example of this article describes the implementation of Python to crack the guessing game algorithm. Share it with everyone for your reference, the details are as follows:

The chat robot in the QQ group will launch a guessing game. The gameplay is as follows:

1. The user sends #guessing the number to the group
2. Robot response: Guessing has started, the range is a number between 1-10000
3. You send #guess[123] to the group
4. Robot response: Is it too big or too small? Yes, or congratulations on your guess
5. You guess a smaller or larger number based on the 123 you just guessed, and return, send #guess number[111], that is, return to step 2

Then the best guessing method is definitely to find the number in the middle. Since mental arithmetic is time-consuming, I directly use the python script to crack this:


#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = &#39;huhu, <huyoo353@126.com>&#39;
def find_middle(start, end):
  #print start, end
  return round((start+end)/2.0)
if __name__ == &#39;__main__&#39;:
  start, end = &#39;&#39;,&#39;&#39;
  text = raw_input(u"> 输入猜数的范围(如:421-499 或者421 499 或者421,499):").decode(&#39;gb18030&#39;)
  spliters = &#39;-, &#39;
  for c in spliters:
    if text.find(c) != -1:
      num_list = text.split(c)
      if &#39;&#39;.join(num_list).isdigit():
        start, end = num_list[0],num_list[1]
        break
  if start == &#39;&#39; or end == &#39;&#39;:
    print u&#39;范围不正确&#39;
  else:
    start = int(start)
    end  = int(end)
    count = 1
    last_guess = find_middle(start,end)
    while 1:
      result = raw_input(u"放弃猜测直接回车, 等于输入=, 小了输入1, 大了请输入2\n>>> #猜数[%d] ,对吗?> " % last_guess ).decode(&#39;gb18030&#39;)
      #print type(text)
      if result in [&#39;q&#39;,&#39;e&#39;,&#39;exit&#39;,&#39;quit&#39;,&#39;bye&#39;,u&#39;退出&#39;]:
        print &#39;Bye!&#39;
        break
      else:
        result=result.strip()
        if result == &#39;1&#39;:
          start = last_guess
          last_guess = find_middle(last_guess,end)
        elif result == &#39;2&#39;:
          end = last_guess
          last_guess = find_middle(start,last_guess)
        elif result == &#39;=&#39;:
          print u&#39;恭喜猜中, 共猜了%d次&#39; % count
          print u&#39;#猜数[%d]&#39; % last_guess
          break
        else: #
          continue
        count += 1
Copy after login

The above is the detailed content of Detailed explanation of examples of cracking guessing games using Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template