http://www.pythonchallenge.com/是一个在线的python过关游戏,一共有33关.玩这个游戏对熟悉python用法及相关库的使用都很有好处.

目前做到了第九关.python版本3.4. 操作系统版本32位win7,64位win7.

0.http://www.pythonchallenge.com/pc/def/0.html

计算2的38次方

1.http://www.pythonchallenge.com/pc/def/274877906944.html

提示已经很清楚了,简单的字符转换.可以使用str模块中的maketrans()和translate()方法.https://docs.python.org/3/library/stdtypes.html#str.translate

2.http://www.pythonchallenge.com/pc/def/ocr.html

页面源码里有注释:

<!--
  find rare characters in the mess below:
  -->

用到了re模块,这里有很好的关于re模块的使用说明,我觉得比官方文档要好读一些http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html

import urllib.request as ur
import re url = "http://www.pythonchallenge.com/pc/def/ocr.html" def main():
global url response = ur.urlopen(url)
body = response.read() text = re.search("<!--\n%(.|\s)+",body.decode())
dic={}
#print(text.group(0))
for x in text.group(0):
if x not in dic:
dic[x] = 1
else:
dic[x] += 1 print(dic)
for i in dic:
if (dic[i]== 1 and 'a' <= i <='z'):
print(i) if __name__ == '__main__':
main()

3.http://www.pythonchallenge.com/pc/def/equality.html

提示:One small letter, surrounded by EXACTLY three big bodyguards on each of its sides.

import urllib.request as ur
import re url = "http://www.pythonchallenge.com/pc/def/equality.html" def main():
response = ur.urlopen(url)
body = response.read() pattern="[^A-Z][A-Z][A-Z][A-Z]([a-z])[A-Z][A-Z][A-Z][^A-Z]" result = re.findall(pattern,body.decode())
print(result) if __name__ == '__main__':
main()

4.http://www.pythonchallenge.com/pc/def/linkedlist.php

查看页面源代码,可以看到提示有follow the chain,又有一个href,点击之后页面显示“next nothing is ....”猜想是不断地访问下一个url直到答案出现.

使用re模块时,注意match()与search()的区别:
Python offers two different primitive operations based on regular expressions: re.match() checks for a match only at the beginning of the string, while re.search() checks for a match anywhere in the string (this is what Perl does by default).

import urllib.request as ur
import re url = "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=8022" def main():
global url
global body
while 1:
print(url)
response = ur.urlopen(url)
#print(type(response)) body = response.read()
#print(type(body))
#print(body)
#print(body.decode()) pattern="and the next nothing is ([0-9]+)"
#match从string的开头查找
#search从string的anywhere开始找
result = re.search(pattern,body.decode())
#print(result)
#m=re.match(r'and the next nothing is ([0-9]+)', body)
#print(m)
if result:
num = result.group(1)
print(result.group(0))
print(num)
url = "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=%s" % num
else:
#print(type(result),result)
break
print(body) if __name__ == '__main__':
main()

5.http://www.pythonchallenge.com/pc/def/peak.html

  查看页面源代码有提示.peak hell sounds familiar.去翻翻标准库,看到一个模块叫pickle的.
  pickle模块用来实现python对象的序列化,反序列化.
import urllib.request as ur
import pickle url = "http://www.pythonchallenge.com/pc/def/banner.p" def main():
global url httpresponse = ur.urlopen(url)
dataBytes = httpresponse.read() obj = pickle.loads(dataBytes)
#print(type(obj),obj)
for ele in obj:
#print(ele)
s=""
for i in ele:
#print(i)
#print(i[0]*i[1])
s += i[0]*i[1]
print(s) if __name__ == '__main__':
main()

6.http://www.pythonchallenge.com/pc/def/channel.html

    查看页面源代码,注释里有zip字样,去标准库里查查看有没有zip相关。
    输入urlhttp://www.pythonchallenge.com/pc/def/channel.zip发现有一个channe.zip文件可以下载
    解压后读一下readme.txt。是类似于04的一直读文件下去,直到出现提示,collect comments。去翻标准库,发现一个zipfile.getinfo()的方法,原来打包的每个文件还可以有注释的,
    还真是第一次知道.

import urllib.request as ur
import zipfile
import pathlib
import re url = "http://www.pythonchallenge.com/pc/def/channel.zip" def main():
# global url #下载zip文件并解压
# httpresponse = ur.urlopen(url)
# dataBytes = httpresponse.read()
# with open("channel.zip", "wb") as local_file:
# if local_file.write(dataBytes):
# z = zipfile.ZipFile("channel.zip")
# z.extractall("./channel") # # p = pathlib.Path("./channel")
# # for files in p.iterdir():
# # #print(files,type(files))
# # #str = open(files).read()
# # str = files.open().read()
# # print(str)
# # break; #读取解压的文档获取提示 z = zipfile.ZipFile("channel.zip")
z.extractall("./channel") orderedFilePath=[]
filepath = "./channel/90052.txt"
filename = "90052.txt"
while 1:
orderedFilePath.append(filename)
f = open(filepath)
content = f.read()
result = re.match("Next nothing is (\d+)",content)
if result:
num = result.group(1)
#print(num)
filepath = "./channel/%s.txt" % num
filename = "%s.txt" % num
else:
#print(content)
break print(orderedFilePath)
for file in orderedFilePath:
c = z.getinfo(file).comment.decode()
print(c,end="") if __name__ == '__main__':
main()

7.http://www.pythonchallenge.com/pc/def/oxygen.html

这一关要用到图形库PIL.首先PIL官方版不支持py3,其次官方版不支持64位python.一番google之后,下载http://pillow.readthedocs.org/en/latest/installation.htmlpillow代替PIL

这题没什么思路,google以后知道是要读那些灰色块的像素值,再将其转码为对应的字符.
import urllib.request as ur
from PIL import Image def ReadUrl(url):
httpresponse = ur.urlopen(url)
dataBytes = httpresponse.read() return dataBytes def main():
url = "http://www.pythonchallenge.com/pc/def/oxygen.png"
databytes = ReadUrl(url)
f = open("oxygen.png","wb")
f.write(databytes)
im = Image.open("oxygen.png")
#print(im.size)
(width,height) = im.size for i in range(0,width,1):
pos = (i,height/2)
#pixel(49,49,49,255)前三位代表的是rgb的值.
pixel = im.getpixel(pos)
#ascii码转字符
char = chr(pixel[0])
print(char,end="") print(''.join(map(chr, [105, 110, 116, 101, 103, 114, 105, 116, 121]))) if __name__ == '__main__':
main()

8.http://www.pythonchallenge.com/pc/def/integrity.html

页面源码里有一个链接,href="../return/good.html",点击之后发现要用户名和密码,恰好当前页面源码里有注释

<!--
  un: 'BZh91AY&SYA\xaf\x82\r\x00\x00\x01\x01\x80\x02\xc0\x02\x00 \x00!\x9ah3M\x07<]\xc9\x14\xe1BA\x06\xbe\x084'
  pw: 'BZh91AY&SY\x94$|\x0e\x00\x00\x00\x81\x00\x03$ \x00!\x9ah3M\x13<]\xc9\x14\xe1BBP\x91\xf08'
  -->
un,pw这不是username,password的缩写吗.最开始认为是和加密解密有关的,看了hashlib模块没有头绪,上网一搜原来是和压缩解压缩有关,BZh91AY&SY这是代表一种bzip2的压缩算法的.应该使用bz2模块..bz后缀的压缩文件还真没见过......
开始时把un,pw作为str处理发现不对,要作为bytes处理
import bz2

un=b'BZh91AY&SYA\xaf\x82\r\x00\x00\x01\x01\x80\x02\xc0\x02\x00 \x00!\x9ah3M\x07<]\xc9\x14\xe1BA\x06\xbe\x084'
pw=b'BZh91AY&SY\x94$|\x0e\x00\x00\x00\x81\x00\x03$ \x00!\x9ah3M\x13<]\xc9\x14\xe1BBP\x91\xf08' def main():
#username = bz2.decompress(un.decode("utf-8"))
username=bz2.decompress(un)
password=bz2.decompress(pw)
print(username,password) if __name__ == '__main__':
main()

9.http://www.pythonchallenge.com/pc/return/good.html

提示1.要将这些点连起来,2.first+second=?
考虑获取点的坐标,重新画图。
import urllib.request as ur
from PIL import Image def SaveImg(url,imgname):
httpresponse = ur.urlopen(url)
dataBytes = httpresponse.read()
f = open(imgname,"wb")
f.write(databytes)
f.close() first = [146,399,163,403,170,393,169,391,166,386,170,381,170,371,170,355,169,346,167,335,170,329,170,320,170,
310,171,301,173,290,178,289,182,287,188,286,190,286,192,291,194,296,195,305,194,307,191,312,190,316,
190,321,192,331,193,338,196,341,197,346,199,352,198,360,197,366,197,373,196,380,197,383,196,387,192,
389,191,392,190,396,189,400,194,401,201,402,208,403,213,402,216,401,219,397,219,393,216,390,215,385,
215,379,213,373,213,365,212,360,210,353,210,347,212,338,213,329,214,319,215,311,215,306,216,296,218,
290,221,283,225,282,233,284,238,287,243,290,250,291,255,294,261,293,265,291,271,291,273,289,278,287,
279,285,281,280,284,278,284,276,287,277,289,283,291,286,294,291,296,295,299,300,301,304,304,320,305,
327,306,332,307,341,306,349,303,354,301,364,301,371,297,375,292,384,291,386,302,393,324,391,333,387,
328,375,329,367,329,353,330,341,331,328,336,319,338,310,341,304,341,285,341,278,343,269,344,262,346,
259,346,251,349,259,349,264,349,273,349,280,349,288,349,295,349,298,354,293,356,286,354,279,352,268,
352,257,351,249,350,234,351,211,352,197,354,185,353,171,351,154,348,147,342,137,339,132,330,122,327,
120,314,116,304,117,293,118,284,118,281,122,275,128,265,129,257,131,244,133,239,134,228,136,221,137,
214,138,209,135,201,132,192,130,184,131,175,129,170,131,159,134,157,134,160,130,170,125,176,114,176,
102,173,103,172,108,171,111,163,115,156,116,149,117,142,116,136,115,129,115,124,115,120,115,115,117,
113,120,109,122,102,122,100,121,95,121,89,115,87,110,82,109,84,118,89,123,93,129,100,130,108,132,110,
133,110,136,107,138,105,140,95,138,86,141,79,149,77,155,81,162,90,165,97,167,99,171,109,171,107,161,
111,156,113,170,115,185,118,208,117,223,121,239,128,251,133,259,136,266,139,276,143,290,148,310,151,
332,155,348,156,353,153,366,149,379,147,394,146,399] second = [156,141,165,135,169,131,176,130,187,134,191,140,191,146,186,150,179,155,175,157,168,157,163,157,159,
157,158,164,159,175,159,181,157,191,154,197,153,205,153,210,152,212,147,215,146,218,143,220,132,220,
125,217,119,209,116,196,115,185,114,172,114,167,112,161,109,165,107,170,99,171,97,167,89,164,81,162,
77,155,81,148,87,140,96,138,105,141,110,136,111,126,113,129,118,117,128,114,137,115,146,114,155,115,
158,121,157,128,156,134,157,136,156,136] def main():
coordinates = first + second
im = Image.open("good.jpg")
w,h = im.size
mode = im.mode
bands = im.getbands()
print(type(mode),mode)
print(type(bands),bands) #两两为一个像素点位置
postions = []
for x in range(0,len(coordinates),2):
postion = (coordinates[x],coordinates[x+1])
postions.append(postion) im2 = Image.new(mode,(w,h))
for pos in postions:
im2.putpixel(pos,(255,255,255))
im2.save('09answer.jpg') if __name__ == '__main__':
main()

Pythonchallenge一起来闯关的更多相关文章

  1. Pythonchallenge一起来闯关(二)

    前情提要:Pythonchallenge一起来闯关(一) 这一篇来闯关10-15.感觉这几关比先前的难了不少,有的题目完全没思路. 10. 页面源码中的链接点击后有a = [1, 11, 21, 12 ...

  2. THEPYTHONCHALLENG闯关记录

    由于是自己看视频学python,总觉得不写几行代码就什么都没有学到. 找了一个写代码的网站其实只是因为这个看起来好玩. 闯关地址http://www.pythonchallenge.com/index ...

  3. 网页闯关游戏(riddle webgame)--H5刮刮卡的原理和实践

    前言: 之前编写了一个网页闯关游戏(类似Riddle Game), 除了希望大家能够体验一下我的游戏外. 也愿意分享编写这个网页游戏过程中, 学到的一些知识. 对于刮刮卡, 想必大家都很熟悉, 也很喜 ...

  4. 网页闯关游戏(riddle webgame)--SQL注入的潘多拉魔盒

    前言: 之前编写了一个网页闯关游戏(类似Riddle Game), 除了希望大家能够体验一下我的游戏外. 也愿意分享编写这个网页游戏过程中, 学到的一些知识. web开发初学者往往会忽视一些常见的漏洞 ...

  5. 网页闯关游戏(riddle webgame)--仿微信聊天的前端页面设计和难点

    前言: 之前编写了一个网页闯关游戏(类似Riddle Game), 除了希望大家能够体验一下我的游戏外. 也愿意分享编写这个网页游戏过程中, 学到的一些知识. 本文讲描述, 如何在网页端实现一个仿微信 ...

  6. 网页闯关游戏(riddle webgame)--游戏玩法和整体介绍

    前言: 记得上大学那会, 有位传说中的大牛, 写了一个网页闯关类的游戏. 当时我们玩得不亦乐乎, 也是第一次接触到这种形式的游戏. 不过当时纯玩家心态, 并没有想过去创造一个. 最近想起这事, 突然想 ...

  7. 《JavaScript 闯关记》

    为何写作此课程 stone 主要负责基于 Web 的企业内部管理系统的开发,虽然能够熟练地使用 JavaScript,但随着对 JavaScript 的理解越来越深,才发现自己尚未掌握其精髓. 201 ...

  8. 淘宝ued - 前端智勇大闯关(第三季)答案(更新)

    淘宝ued - 前端智勇大闯关(第三季)答案(更新) 下午在微博上看到了淘宝智勇大闯关第三季的信息,感觉挺有意思的,于是就尝试做了下.附上题目地址: http://ued.campus.alibaba ...

  9. 《JavaScript闯关记》视频版硬广

    <JavaScript闯关记>视频版硬广 stone 在菜航工作时,兼任内部培训讲师,主要负责 JavaScript 基础培训,2016年整理的<JavaScript闯关记>课 ...

随机推荐

  1. 快学Scala第一部分

    转载: 1.变量声明 val answer = 8 * 5 + 2; //常量 var counter = 0;    //变量 //在必要的时候 ,可以指定类型 val greeting:Strin ...

  2. POJ 3436 ACM Computer Factory

    题意:   为了追求ACM比赛的公平性,所有用作ACM比赛的电脑性能是一样的,而ACM董事会专门有一条生产线来生产这样的电脑,随着比赛规模的越来越大,生产线的生产能力不能满足需要,所以说ACM董事会想 ...

  3. wpa_supplicant使用笔记-wpa_cli iwconfig

    还在搞8634,看不到头了..唉.wireless_tools的缺点是不支持WPA认证,所以有必要把wpa_supplicant也移植过来.无线 网卡是基于zydas芯片的,正好可以在网上搜到wpa_ ...

  4. Linux下设置文件权限

    文件权限示意图: 第一步:在终端创建用户 增加用户 useradd 用户名 设置密码 passwd 用户名 通过上述两条命令创建a1,a2两个用户. 第二步:在根目录使用管理员账号创建一个文件 在使用 ...

  5. zookeeper 删除snapshot和transaction log的源码解读

    转载请注明源地址http://www.cnblogs.com/dongxiao-yang/p/4910059.html zookeeper具有自动清除快照日志和事务日志的工能,可以在配置文件设置aut ...

  6. android设置view透明度的效果

    android设置view透明度的效果 推荐textView.setBackgroundColor(Color.TRANSPARENT);     第一种方法:在xml文件中设置背景颜色. andro ...

  7. [PWA] 0. Introduce to Offline First

    Why offline first? Imagin you are visiting a website, it is fine if wifi connection is good. It migh ...

  8. 用htaccess进行访问控制(转)

    1. 文件访问控制 利用 httpd.conf 中的 Order.Files 及 FilesMatch 命令实现的访问控制可以满足大部分要求,但是当用户被拒绝时,他们看到的是硕大的“403 Forbi ...

  9. C++之智能指针

    导读 一直对智能指针有一种神秘的赶脚,虽然平时没怎么用上智能指针,也就看过STL中的其中一种智能指针auto_ptr,但是一直好奇智能指针的设计因此,今天看了一下<C++ Primer Plus ...

  10. 一行代码实现iOS序列化与反序列化(runtime)

    一.变量声明 为便于下文讨论,提前创建父类Biology以及子类Person: Biology: @interface Biology : NSObject { NSInteger *_hairCou ...