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

Test your python mastery through game play

Jun 03, 2019 pm 06:00 PM
python

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

Is the vscode extension malicious? Is the vscode extension malicious? Apr 15, 2025 pm 07:57 PM

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

How to run programs in terminal vscode How to run programs in terminal vscode Apr 15, 2025 pm 06:42 PM

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Can visual studio code be used in python Can visual studio code be used in python Apr 15, 2025 pm 08:18 PM

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

Can vscode be used for mac Can vscode be used for mac Apr 15, 2025 pm 07:36 PM

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

Can vscode run ipynb Can vscode run ipynb Apr 15, 2025 pm 07:30 PM

The key to running Jupyter Notebook in VS Code is to ensure that the Python environment is properly configured, understand that the code execution order is consistent with the cell order, and be aware of large files or external libraries that may affect performance. The code completion and debugging functions provided by VS Code can greatly improve coding efficiency and reduce errors.

See all articles