【问题标题】:Name Error in Python, even when name is definedPython中的名称错误,即使定义了名称
【发布时间】:2015-04-04 14:43:15
【问题描述】:

所以我的函数使用了 3 个参数。我创建了一个使用这三个之一的辅助函数。助手工作得很好。 但是,当我在主函数中调用它时,它给了我名称错误,并说参数的名称未定义。

助手创建一个列表。所以我称之为

new_list = helper_function(parameter)

它不起作用。

这是一个非常混乱的代码,因为我目前正在处理它

#HELPERS
def create_list_of_vowels(phonemes):
    '''
    Returns a list of vowels from all phonemes
    >>>create_list_of_vowels(['AE1', 'B', 'S', 'IH0', 'N', 'TH']):
    ['AE1', 'IH0']
    '''
    number = ('0', '1', '2')
    vowels = []
    for i in phonemes:
        if i[-1] in number:
            vowels.append(i)
    return vowels

def clean_up(s):
    """ (str) -> str

    Return a new string based on s in which all letters have been
    converted to uppercase and punctuation characters have been stripped
    from both ends. Inner punctuation is left untouched.

    >>> clean_up('Birthday!!!')
    'BIRTHDAY'
    >>> clean_up('"Quoted?"')
    'QUOTED'
    """

    punctuation = """!"'`@$%^&_-+={}|\\/,;:.-?)([]<>*#\n\t\r"""
    result = s.upper().strip(punctuation)
    return result

def every_line_to_list(poem_lines):
    """
    Return every string converted into a list of strings

    >>>every_line_to_list(['The first line leads off,', 'With a gap before the next.', 'Then the poem ends.'])
    [['The', 'first', 'line', 'leads', 'off,'], ['With', 'a', 'gap', 'before', 'the', 'next'], ['Then', 'the', 'poem', 'ends.']
    >>>every_line_to_list()
    """
    return [x.split() for x in poem_lines]
#========================================================


def check_vowel_phoneme_counts(poem_lines, pattern, word_to_phonemes):
    r""" (list of str, poetry pattern, pronunciation dictionary) -> list of str

    Precondition: len(poem_lines) == len(pattern[0])

    Return a list of lines from poem_lines that do not have the right number of
    vowel phonemes for the poetry pattern according to the pronunciation dictionary.
    If all lines have the right number of vowel phonemes, return the empty list.

    >>> poem_lines = ['The first line leads off,', 'With a gap before the next.', 'Then the poem ends.']
    >>> pattern = ([5, 5, 4], ['*', '*', '*'])
    >>> word_to_phonemes = {'NEXT': ['N', 'EH1', 'K', 'S', 'T'],
    ...                     'GAP': ['G', 'AE1', 'P'],
    ...                     'BEFORE': ['B', 'IH0', 'F', 'AO1', 'R'],
    ...                     'LEADS': ['L', 'IY1', 'D', 'Z'],
    ...                     'WITH': ['W', 'IH1', 'DH'],
    ...                     'LINE': ['L', 'AY1', 'N'],
    ...                     'THEN': ['DH', 'EH1', 'N'],
    ...                     'THE': ['DH', 'AH0'], 
    ...                     'A': ['AH0'], 
    ...                     'FIRST': ['F', 'ER1', 'S', 'T'], 
    ...                     'ENDS': ['EH1', 'N', 'D', 'Z'],
    ...                     'POEM': ['P', 'OW1', 'AH0', 'M'],
    ...                     'OFF': ['AO1', 'F']}
    >>> check_vowel_phoneme_counts(poem_lines, pattern, word_to_phonemes)
    ['With a gap before the next.', 'Then the poem ends.']
    >>> poem_lines = ['The first line leads off,']
    >>> check_vowel_phoneme_counts(poem_lines, ([0], ['*']), word_to_phonemes)
    []
    """
#split into list of lists 

new_poem_lines = every_line_to_list(poem_lines)

# CLEAN UP STRINGS (REMOVE PUNCTUATION)

for line1 in new_poem_lines:
    for word in line1:
        word = clean_up(word)

# Find each word in the string in the word_to_phonemes
# return list of all vowels, calculate them
# if number of vowels does not equal to pattern[i], append to list

================================================ ====

错误信息:

3.4.2 (v3.4.2:ab2c023a9432, Oct  6 2014, 22:15:05) [MSC v.1600 32 bit (Intel)]
Python Type "help", "copyright", "credits" or "license" for more information.
poem_lines = ['The first line leads off,', 'With a gap before the next.', 'Then the poem ends.']
check_vowel_phoneme_counts(poem_lines, pattern, word_to_phonemes)
Traceback (most recent call last):
  File "<string>", line 1, in <fragment>
builtins.NameError: name 'check_vowel_phoneme_counts' is not defined

【问题讨论】:

  • 请发布您的代码和错误
  • 那么你在哪里定义了parameter在调用代码中?您在这里为该函数参数传递了一个值,因此您实际上需要传递一些东西。
  • LittlePanda 说的:我们无法帮助您调试我们看不到的代码。所以发一个MCVE
  • 已发布,对此感到抱歉
  • 您能否在问题中包含完整错误消息?

标签: python list helper nameerror


【解决方案1】:

问题是没有定义变量'poem_lines'。在将其作为参数传递给函数 every_line_to_list() 之前,您需要为其分配一些内容。示例:

poem_lines = ['The first line leads off,', 'With a gap before the next.', 'Then the poem ends.']
new_poem_lines = every_line_to_list(poem_lines)

【讨论】:

    【解决方案2】:

    你定义 check_vowel_phoneme_counts(...):

    但是你没有在你的定义下缩进的代码。如果你的文档字符串下面的代码应该在函数中,那么它必须缩进。

    Python 不允许您定义没有语句的函数。

    def check_vowel_phoneme_counts(poem_lines, pattern, word_to_phonemes):
    #split into list of lists 
    
    new_poem_lines = every_line_to_list(poem_lines)
    
    for line1 in new_poem_lines:
        for word in line1:
            word = clean_up(word)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多