2013-09-23 55 views
-1

我在Python得到一个语法错误,错误:蟒蛇回报失去作用

SyntaxError: 'return' outside function 

这似乎是不言自明,但据我可以看到回报是功能。

这里是我的代码:

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 

我显然失去了一些东西,我只是想不通什么

回答

7

我怀疑你在混合标签和空格..你的def在它前面有4个空格,随后你使用多个标签进行缩进。

PEP 8建议使用(4spaces over tabs

还要注意从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!

+0

啊,你说得对,干杯! – DasSnipez