【问题标题】:python return out of functionpython返回函数
【发布时间】:2013-09-27 20:57:36
【问题描述】:

我在 Python 中遇到语法错误,错误:

SyntaxError: 'return' outside function

这似乎很容易解释,但就 可以看到 return 是 inside 一个函数。

这是我的代码:

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

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

【问题讨论】:

    标签: python function syntax return


    【解决方案1】:

    我怀疑您正在混合制表符和空格..您的 def 前面有 4 个空格,随后您使用多个制表符进行缩进。

    PEP 8 建议使用 (4) spaces over tabs

    还要注意 PEP 8 中的以下内容:

    Python 3 不允许在缩进中混合使用制表符和空格。

    混合使用制表符和空格缩进的 Python 2 代码应该是 转换为独占使用空格。

    使用 -t 调用 Python 2 命令行解释器时 选项,它会发出有关非法混合选项卡和代码的警告 空格。使用 -tt 时,这些警告会变成错误。这些选项是 强烈推荐!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-10
      • 2019-02-12
      • 2014-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多