DasSnipez
DasSnipez

Reputation: 2314

python return out of function

I'm getting a syntax error in Python, the error:

SyntaxError: 'return' outside function

That seems pretty self-explanatory but as far as I can see return is inside a function.

Here is my code:

def getLinks(self, url, fandom, soup):
    links = []

    searchElementDict = {
    'aff':'select', 'fcwd':'select', 'ffn':'select', 'tthm':'select', 'lua':'select', 'ffa':'select', 
    'hpfd':'select', 'phns':'select', 'mbba':'div', 'avgf':'div', 'mugn':'select', 'hpffa':'select',
    'hpff':'select',
    }
    if fandom in searchElementDict:
        searchElement = searchElementDict[fandom]

    searchElementForDict = {
    'aff':'name', 'fcwd':'name', 'ffn':'title', 'tthm':'name', 'lua':'class', 'ffa':'class',
    'hpfd':'name', 'phns':'name', 'mbba':'id', 'avgf':'id', 'mugn':'name', 'hpffa':'name',
    'hppf':'name',
    }
    if fandom in searchElementForDict:
        searchElementFor = searchElementForDict[fandom]

    withValueDict = {
    'aff':'chapnav', 'fcwd':'goto', 'ffn':'Chapter Navigation', 'tthm':'chapnav', 'lua':'textbox',
    'ffa':'locationSelect', 'hpfd':'sid', 'phns':'chao', 'mbba':'mibba-layout-parts', 'avgf':'chapters',
    'mugn':'chapter', 'hpffa':'chapter', 'hpff':'chapterid',
    }
    if fandom in withValueDict:
        withValue = withValueDict[fandom]   
    try:    
        if fandom == 'mbba' or fandom == 'avgf':
            chapterGroup = soup.find(searchElement, attrs={searchElementFor : withValue})
            individualChapters = chapterGroup.findAll('a')
            for each in individualChapters:         
                chapterLink = each['href']
                links.append(chapterLink)       
        else:   
            chapterGroup = soup.find(searchElement, attrs={searchElementFor : withValue})
            individualChapters = chapterGroup.findAll('option', attrs={'value=':''})
            for each in individualChapters:         
                chapterLink = each.get('value')
                links.append(chapterLink)
            if fandom == 'fcwd':
                del links[0]
            elif fandom == 'hpfd' or fandom == 'hpff':
                del links[0]
                del links[0]
    except:
        links.append(1) 


    return links

I'm obviously missing something, I just can't figure out what.

Upvotes: 0

Views: 2738

Answers (1)

Levon
Levon

Reputation: 143102

I suspect you are mixing tabs and spaces .. your def has 4 spaces preceding it, subsequently you are using multiple tabs for indentation.

PEP 8 recommends use of (4) spaces over tabs.

Also note the following from PEP 8:

Python 3 disallows mixing the use of tabs and spaces for indentation.

Python 2 code indented with a mixture of tabs and spaces should be converted to using spaces exclusively.

When invoking the Python 2 command line interpreter with the -t option, it issues warnings about code that illegally mixes tabs and spaces. When using -tt these warnings become errors. These options are highly recommended!

Upvotes: 8

Related Questions