所以我的函數使用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
發佈您的代碼和錯誤請 – LittlePanda 2015-04-04 14:44:08
那麼你在調用代碼*中定義了'parameter' *的位置?你在這裏傳遞了一個函數參數的值,所以你需要實際上有東西傳入。 – 2015-04-04 14:44:43
LittlePanda說:我們不能幫你調試我們看不到的代碼。所以_please_發佈[MCVE](http://stackoverflow.com/help/mcve)。 – 2015-04-04 14:51:41