Python Forum
function returning None - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: function returning None (/thread-273.html)



function returning None - tkj80 - Oct-03-2016

Hi,

I'm working my way through an example in a book. However, it is return an unexpected value. Wonder if someone could help point out what/where I did wrong.

baseUrl ="http://pythonscraping.com" 
source = "www.pythonscraping.com/sites/default/files/lrg_0.jpg"

def getAbsoluteURL(baseUrl, source):
	if source.startswith("http://www."):
		url = "http://"+source[11:]
	elif source.startswith("http://"):
		url = source
	elif source.startswith("www."):
		url = source[4:]
		url = "http://"+source
	else:
		url = baseUrl+"/"+source
	if baseUrl not in url:
		return None
	return url
When I run the following code:
fileUrl = getAbsoluteURL(baseUrl, source)
print(fileUrl)
I got the value None. I thought that the second elif statement would evaluate to True, in which the url returned would be "http://pythonscraping.com/sites/default/files/lrg_0.jpg" as opposed to None.
I tried to trace where it went wrong by only defining the function as:
def getAbsoluteURL(baseUrl, source):
	if source.startswith("http://www."):
		url = "http://"+source[11:]
	elif source.startswith("http://"):
		url = source
	elif source.startswith("www."):
		url = source[4:]
		url = "http://"+source
However, the return value still is None.


RE: function returning None - metulburr - Oct-03-2016

IF you add a couple prints you can tell that baseUrl is indeed not in url
def getAbsoluteURL(baseUrl, source):
    if source.startswith("http://www."):
        url = "http://"+source[11:]
    elif source.startswith("http://"):
        url = source
    elif source.startswith("www."):
        url = source[4:]
        url = "http://"+source
    else:
        url = baseUrl+"/"+source
    print(baseUrl)
    print(url)
    if baseUrl not in url:
        return None
    return url
Output:
http://pythonscraping.com http://www.pythonscraping.com/sites/default/files/lrg_0.jpg None



RE: function returning None - wavic - Oct-04-2016

The second elif evaluate to True but the slicing is wrong. Your slice starts from the 5th element to the end of the string. Remember that in Python indexing starts from 0 so 4 is the 5th element. So the url variable is messed up which leads to the last if to be True and this returns None.


RE: function returning None - tkj80 - Oct-06-2016

I looked more closely, found that that code in the book was incorrect and it should be instead.

 elif source.startswith("www."):

        url = "http://"+source[4:]
Thank you for your help!