所以我的功能使用了3个参数。我创建了一个使用这三个中的一个的辅助函数。助手工作得非常好。 但是,当我在main函数中调用它时,它会给出名称错误,并且表示未定义参数的名称。
帮助程序创建一个列表。所以我称之为
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
答案 0 :(得分: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)
答案 1 :(得分: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)