what's going on here?

John Salerno johnjsal at NOSPAMgmail.com
Mon Apr 3 23:41:04 EDT 2006


Ok, long story: I'm trying to solve level 4 of the Python Challenge. I 
hate to post here, but the hint forum over there is dead. Here's the 
link: http://www.pythonchallenge.com/pc/def/linkedlist.php

Apparently you need to use a linked list to solve it, so I read up on 
them but I still don't understand how to implement one to solve this 
problem. (Any hints there would be appreciated too.) So I wrote this 
code instead. It goes to each URL, reads the source code, gets the next 
number, etc. I realize it uses some terrible tricks, like the regex and 
the try/except clause, which is way too specific to solve the problem in 
a general way. Anyway, here's the code:

# Python Challenge, level 4

import urllib
import re

url = 'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing='
pattern = re.compile(r'\bnext nothing is (\d+)')

def getNextNum(url):
     global pattern
     global nextNum
     site = urllib.urlopen(url)
     try:
         source = site.read()
         site.close()
         number = re.search(pattern, source).group(1)
     except:
         print nextNum
         number = str(int(nextNum) / 2)
     return number

nextNum = '12345'
f = open(r'C:\Python24\myscripts\challenges\numbers.txt', 'w')
try:
     for x in range(300):
         f.write(nextNum + '\n')
         nextNum = getNextNum(url + nextNum)
finally:
     f.close()

print url + nextNum

Now, I tried this earlier on my work computer and it got as far as 
printing out two 'nextNum's in the except block, then it appeared that 
the website timed out and I could no longer access it for a while.

I tried later on my home computer and now I keep getting this error:

Traceback (most recent call last):
   File "C:\Python24\myscripts\challenges\linked_list.py", line 27, in 
-toplevel-
     nextNum = getNextNum(url + nextNum)
   File "C:\Python24\myscripts\challenges\linked_list.py", line 12, in 
getNextNum
     site = urllib.urlopen(url)
   File "C:\Python24\lib\urllib.py", line 82, in urlopen
     return opener.open(url)
   File "C:\Python24\lib\urllib.py", line 190, in open
     return getattr(self, name)(url)
   File "C:\Python24\lib\urllib.py", line 322, in open_http
     return self.http_error(url, fp, errcode, errmsg, headers)
   File "C:\Python24\lib\urllib.py", line 339, in http_error
     return self.http_error_default(url, fp, errcode, errmsg, headers)
   File "C:\Python24\lib\urllib.py", line 579, in http_error_default
     return addinfourl(fp, headers, "http:" + url)
   File "C:\Python24\lib\urllib.py", line 871, in __init__
     addbase.__init__(self, fp)
   File "C:\Python24\lib\urllib.py", line 818, in __init__
     self.read = self.fp.read
AttributeError: 'NoneType' object has no attribute 'read'

I didn't get this before, so I wonder if it's a website error. There 
seems to be a problem with getting the source code. It also appears that 
the script, when run at home, doesn't get as far as the same script when 
I ran it earlier today at work.

So I figure it's either a website problem, or some kind of strange 
difference between the two computers I'm using (but both use 2.4.3 and 
nothing else seems different).

I hope someone can point me in the right direction. I'm curious why it 
fails in a different way at home than at work, but also I'd like to know 
if it's even possible to solve the problem in this way, or if I *have* 
to use a linked list.



More information about the Python-list mailing list