首页 > 解决方案 > 使用 Pylint 检查我的代码并出现错误消息

问题描述

通过 pylint 检查我的 python 代码时,出现以下错误消息: source.py:9:4: R1702: Too many nested blocks (7/5) (too-many-nested-blocks) 并且不确定如何调整代码以便它通过了pylint检查。

def pos(sentence):
    """returns sentence with no stems"""
    stemmedSentence=''
    words = sentence.split(" ")
    vowels = ['a', 'e', 'i', 'o', 'u', 'y']
    vowelFound = False
    stemmedWord = ''
    for word in words:
        if '\'s' in word:
            stemmedWord = word.replace('\'s', "")
        elif 's\'' in word:
            stemmedWord = word.replace('s\'', "")
        else:
            stemmedWord = word
        if stemmedWord.endswith('s'):
            if word[-2] not in vowels:
                for c in word:
                    if c in vowels:
                        vowelFound = True
                        break
                if vowelFound:
                    if not word.endswith('us') or word.endswith('ss'):
                        if word.endswith('sses'):
                            stemmedWord = word.replace("sses","ss")
                        elif word.endswith('ies'):
                            stemmedWord = word.replace('ies', 'i')
                            if len(stemmedWord) <= 2:
                                stemmedWord = word
                        else:
                            stemmedWord = word.replace('s', '')
                else:
                    stemmedWord = word
            else:
                stemmedWord = word
        elif word.endswith('ied'):
            stemmedWord = word.replace("ied", "i")
            if len(stemmedWord) <= 2:
                stemmedWord = word.replace("d", "")
        elif word.endswith('ed'):
            stemmedWord = word.replace("ed", "")
        if word.endswith('ing'):
            stemmedWord = word.replace("ing","")
            if len(stemmedWord) < 3:
                stemmedWord = word
        if stemmedWord.endswith('ly'):
            stemmedWord = stemmedWord.replace("ly", "")
        if stemmedWord.endswith('er'):
            stemmedWord = stemmedWord.replace("er", "")
        stemmedSentence += stemmedWord + " "
    return stemmedSentence

标签: python

解决方案


推荐阅读