2015-04-04 83 views
-1

所以我的函数使用3个参数。我创建了一个使用这三者之一的帮助函数。帮手完美无缺。 但是,当我在主函数中调用它时,它给了我名称错误,并且说参数的名称未定义。名称Python中的错误,即使名称已定义

助手创建一个列表。所以我把它叫做

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 
+1

发布您的代码和错误请 – LittlePanda 2015-04-04 14:44:08

+0

那么你在调用代码*中定义了'parameter' *的位置?你在这里传递了一个函数参数的值,所以你需要实际上有东西传入。 – 2015-04-04 14:44:43

+0

LittlePanda说:我们不能帮你调试我们看不到的代码。所以_please_发布[MCVE](http://stackoverflow.com/help/mcve)。 – 2015-04-04 14:51:41

回答

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) 
0

你定义check_vowel_phoneme_counts(...):

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

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)