python退出功能 [英] python return out of function

查看:285
本文介绍了python退出功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  SyntaxError:'return'外部函数

这似乎很好理解,但只要 I 可以看到return是里面一个函数。



这里是我的代码:

  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',
}
如果在searchElementDict中有粉丝:
searchElement = searchElementDict [fandom]

searchElementForDict = {
'aff':'name','fcwd' :'name','ffn':'标题','tthm' :'name','lua':'class','ffa':'class',
'hpfd':'name','phns':'name','mbba':'id',' avgf':'id','mugn':'name','hpffa':'name',
'hppf':'name',
}
如果在searchElementForDict中有粉丝:
searchElementFor = searchElementForDict [fandom]

withValueDict = {
'aff':'chapnav','fcwd':'goto','ffn':'章节导航',' tthm':'chapnav','lua':'textbox',
'ffa':'locationSelect','hpfd':'sid','phns':'chao','mbba':'mibba- 'chapter','hpff':'chapterid',
}
如果粉丝在withValueDict:
withValue = withValueDict [fandom]
try:
if fandom =='mbba'or fandom =='avgf':
chapterGroup = soup.find( searchElemen t,attrs = {searchElementFor:withValue})
individualChapters = chapterGroup.findAll('a')
对于每个单独的章节:
chapterLink = each ['href']
链接.append(chapterLink)
else:
chapterGroup = soup.find(searchElement,attrs = {searchElementFor:withValue})
individualChapters = chapterGroup.findAll('option',attrs = {'value =':''})
在单个章节中:
chapterLink = each.get('value')
links.append(chapterLink)
if fandom =='fcwd ':
del链接[0]
elif fandom =='hpfd'或者fandom =='hpff':
del链接[0]
del链接[0]
除外:
links.append(1)


返回链接

我显然是错的唱歌的东西,我只是无法弄清楚 我怀疑你正在混合标签和空格..你的 def 在它前面有4个空格,随后你使用多个标签进行缩进。



PEP 8 建议使用( 4 标签上的空格



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


Python 3不允许混合使用制表符和空格进行缩进。 / p>

Python 2代码缩进,并带有制表符和空格的混合,应该将
转换为仅使用空格。



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



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.

解决方案

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!

这篇关于python退出功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆