gist: https://gist.github.com/genesislive/5097515
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# http://www.pythonchallenge.com/pc/def/linkedlist.php
import urllib, re
uri = 'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=%s'
nothing = '12345' # '8022'
pattern = 'and the next nothing is (\d+)'
while True:
try:
content = urllib.urlopen(uri % nothing).read()
print content
nothing = re.search(pattern, content).group(1)
# print nothing
except AttributeError:
# AttributeError: 'NoneType' object has no attribute 'group'
# if you see 'Yes. Divide by two and keep going.', just div
# the latest nothing by 2 and try again.
break
print nothing