python challenge_6

第六题题目:http://www.pythonchallenge.com/pc/def/channel.zip

老规矩,先看源代码,源码给出的hintzip我以为是需要用到zip,网上看攻略说把zip换成html,然后就能下载一个zip的压缩包,剩下的东西和第四题比较像,基本上就是从一个文档得到下一个文档的路径,从哪一个开始应该无所谓吧,但是readme里面有写开始的数据,就按照里面的数据作为开头了。

import re
import zipfile
number = '90052'
block = []
while True:
    f = open(r'E:\download\channel\%s.txt' % number)
    data = f.read()
    block = data.split(' ')
    print 'number: %s' % number
    print 'data: %s' % data
    f.close()
    if block[3]:
        number = block[-1]
    else:
        Break

得到的结果是Collect the comments 。 到这个地方有开始卡住了,中文意思是收集评论,网上看攻略,说要用到zipfile里面的内容,然后写了以下的版本,在循环结束,找不到文件路径会报错,没有办法得到comment的内容。

import re
import zipfile
number = '90052'
block = []
zf = zipfile.ZipFile(r'E:\download\channel.zip')
zc = []
                     
while True:
    f = open(r'E:\download\channel\%s.txt' % number)
    data = f.read()
    block = data.split(' ')
    f.close()
    zc.append(zf.getinfo(number+'.txt').comment)
    if block[-1] :
        number = block[-1]
    else:
        break
    
print ''.join(zc)

判断语句改成是否为数字,就能得到结果了。最终代码:

import re
import zipfile
number = '90052'
block = []
zf = zipfile.ZipFile(r'E:\download\channel.zip')
zc = []
                    
while True:
    f = open(r'E:\download\channel\%s.txt' % number)
    data = f.read()
    block = data.split(' ')
    f.close()
    zc.append(zf.getinfo(number+'.txt').comment)
    if block[-1].isdigit() :
        number = block[-1]
    else:
        break
    
print ''.join(zc)

运行结果:

python challenge_6_第1张图片

所以,得到下一题的地址:http://www.pythonchallenge.com/pc/def/hockey.html

正常情况下这就是正确答案了,结果,网页上给了提示it's in the air. look at the letters.看了一下组成的字母,是oxygen

所以正确的地址是:http://www.pythonchallenge.com/pc/def/oxygen.html

你可能感兴趣的:(python challenge_6)