Reputation: 404
Hi everyone I am doing the python challenge at pythonchallenge.com and i am currently at challenge 4. And I have the following code:
import urllib,re
number = 12345
url = 'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=' + str(number)
page = urllib.urlopen(url)
def nextNumber(site):
contents = page.read()
decimal = re.search(r'\d+', contents).group()
while decimal in contents and decimal:
new_url = 'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=' + str(decimal)
print new_url
page2 = urllib.urlopen(new_url)
contents = page2.read()
decimal = re.search(r'\d+', contents).group()
nextNumber(url)
the problem I have is that when I get to the number 16044 the site says I have to divide it by two so decimal equals None and that gives an error. I tried to solve it with some if statements like:
if decimal is None:
print "hi"
but I still get this error.
Upvotes: 0
Views: 296
Reputation: 2370
Your problem is in this statement (in the while loop):
decimal = re.search(r'\d+', contents).group()
re.search
returns None if no match was found.
Try this instead:
decimal = re.search(r'\d+', contents)
if decimal:
decimal = decimal.group()
else:
# do something else
Upvotes: 2