Home > Backend Development > Python Tutorial > Test your python mastery through game play

Test your python mastery through game play

爱喝马黛茶的安东尼
Release: 2019-06-03 18:00:24
Original
4119 people have browsed it

I recently saw a very interesting Python game clearance website on the Internet. There are 33 levels in total. Each level requires you to use Python knowledge to solve problems to find the answer, and then enter the next level. It tests the comprehensive mastery of Python. For example, some levels require the use of regular expressions, and some require the use of crawlers.

Test your python mastery through game play

We usually learn Python in chapter order, packages or modules, and it is easy to forget after learning. You can use this website to comprehensively test your mastery of Python so that you can find and fill in any gaps.

Let’s talk about how to play this website.

Test your python mastery through game play

This is the main page of the website. It has a sense of history, right? It has been in existence for more than ten years. But don’t underestimate it just because it looks like an antique.

Test your python mastery through game play

Let's have some fun, click "get challenged" to start the challenge.

Level 0 is the Warming up warm-up session:

The requirement for this level is to modify the URL link, and the prompt given is a mathematical expression on the computer: 2 to the 38th power, So you probably need to calculate the value, and then modify the url to enter the next level.

So this level is a test of basic numerical operations in Python. Do you know how to calculate?

Open Python's own terminal, and you can calculate the result with one line of code:

Test your python mastery through game play

Replace the 0 in the original link with 274877906944 and press Enter to enter the next page Level 1:

Test your python mastery through game play

#The game officially begins. The notebook in the picture gives three groups of letters, and it is easy to find the pattern: the letters in front are moved back two places to become the letters in the back.

Then what needs to be done is to decrypt the following prompt string according to this rule and perform displacement decryption to get the real sentence meaning:

This question examines the knowledge related to string encoding and for loops, code The implementation is as follows:

text = '''g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq
    ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q
    ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq()
    gq pcamkkclbcb. lmu ynnjw ml rfc spj.'''
text_translate = ''
for i in text:
    if str.isalpha(i):
        n = ord(i)
        if i >= 'y':
            n = ord(i) + 2 - 26
        else:
            n = ord(i) + 2
        text_translate += chr(n)
    else:
        text_translate += i
print(text_translate)
Copy after login

Get the result:

i hope you didnt translate it by hand. 
thats what computers are for. 
doing it in by hand is inefficient and that's why this text is so long. 
using string.maketrans()is recommended. now apply on the url.
Copy after login

The author is very interesting. Of course, you cannot manually calculate it. It is recommended to use string.maketrans() to solve it. What we have adopted above is more direct. method, the official provides a more streamlined method:

import string
l = string.lowercase
t = string.maketrans(l, l[2:] + l[:2])
print (text.translate(t))
Copy after login

Then change the map in the url to ocr and press Enter to reach the second level:

Test your python mastery through game play

The author went on to say that the Test your python mastery through game play hint may be in the book (of course it is impossible) or it may be in the source code of the web page. Then right-click to view the source code and scroll down to see the green area. Sure enough, the problem is found:

Test your python mastery through game play

means: find the fewest characters in the following string of characters. Characters

Inspected several knowledge points:

Regular expression to extract string

list counting

Conditional statement

If it were you, what would you do?

Let’s take a look, ten lines of code to quickly implement:

import requests
url = 'http://www.pythonchallenge.com/pc/def/ocr.html'
res = requests.get(url).text
text = re.findall(&#39;.*?<!--.*-->.*<!--(.*)-->&#39;,res,re.S)
# list转为str便于遍历字符
str = &#39;&#39;.join(text)
lst = []
key=[]
#遍历字符
for i in str:
    #将字符存到list中
    lst.append(i)
    #如果字符是唯一的,则添加进key
    if i not in key:
        key.append(i)
# 将list列表中的字符出现字数统计出来
for items in key:
    print(items,lst.count(items))
Copy after login

First, use Requests to request the web page and then use regular expressions to extract the string, and then use a for loop to count the number of occurrences of each character.

% 6104
$ 6046
@ 6157
_ 6112
^ 6030
# 6115
) 6186
& 6043
! 6079
+ 6066
] 6152
* 6034
} 6105
[ 6108
( 6154
{ 6046
e 1
q 1
u 1
a 1
l 1
i 1
t 1
y 1
Copy after login

You can see that the last few characters appear the least, which together are "equality". By replacing the url characters, you can pass the second level and enter the next level to continue the challenge. Isn’t it interesting?

Each subsequent level requires the use of relevant Python skills to solve, such as level 4:

Test your python mastery through game play

The author of this level made a little prank, You need to manually enter the value into the URL and press Enter. Do you think that's the end of it? It does not constantly pop up new values ​​for you to enter, seemingly endlessly.

Test your python mastery through game play

所以,这一关肯定不能采取手动输入的方法闯关,自然要用到 Python 了。要实现自动填充修改 url 回车跳转到新 url,循环直到网页再也无法跳转为止这一功能。

如果是你,你会怎么做?

其实,一段简单的爬虫加正则就能搞定。思路很简单,把每次网页中的数值提取出来替换成新的 url 再请求网页,循环下去,代码实现如下:

import requests
import re
import os
# 首页url
resp = requests.get(
   &#39;http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345&#39;).text
    url = &#39;http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=&#39;
# 计数器
count = 0
while True:
   try:
    # 提取下一页动态数值
       nextid = re.search(&#39;\d+&#39;, resp).group()
       count = count + 1
       nextid = int(nextid)
    except:
       print(&#39;最后一个url为:%s&#39; % nexturl)
       break
    # 获取下一页url
    nexturl = url + str(nextid)
    print(&#39;url %s:%s&#39; % (count, nexturl))
    # 重复请求
    resp = requests.get(nexturl).text
Copy after login

输出结果如下:

Test your python mastery through game play

可以看到,最终循环了 85 次找到了最后一个数字16044,输入到 url 中就闯关成功。

如果遇到不会做的题,可以在这里找到参考答案:

中参考文教程:

https://www.cnblogs.com/jimnox/archive/2009/12/08/tips-to-python-challenge.html

官方参考教程:

http://garethrees.org/2007/05/07/python-challenge/

The above is the detailed content of Test your python mastery through game play. 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