Python 中的计划
Program in Python
这是我用来制作 pig-latin 翻译器的代码,但我似乎无法翻译它,有人能给我一些提示吗?
我认为我的问题出在编码中并从元音部分开始,但我似乎无法弄清楚。
您忘记在 translate
函数中赋值:
必须是:
phrase = ' '.join(encode(message))
return phrase
除了@delimitry的回答外,还要将第二个函数的if条件中的words改为word,即改变-
if starts_with_vowel(words):
至 -
if starts_with_vowel(word):
调整这3个功能:
def starts_with_vowel(word):
# return True if the word starts with a vowel and False otherwise
return word[0] in ['a', 'e', 'i', 'o', 'u']
def encode(word):
# translate a single word to the secret language
# call starts with vowel to decide which pattern to follow
if starts_with_vowel(word):
return word[1:] + word[0] + 'ar'
else:
return word + 'way'
def translate(message):
# translate the whole text to the secret language
# call encode to translate individual words in text
return ' '.join(encode(word) for word in message)
最大的问题是 encode()
和 starts_with_vowel()
遍历所有单词(但您的评论说它应该适用于 单个单词 )
这是我用来制作 pig-latin 翻译器的代码,但我似乎无法翻译它,有人能给我一些提示吗?
我认为我的问题出在编码中并从元音部分开始,但我似乎无法弄清楚。
您忘记在 translate
函数中赋值:
必须是:
phrase = ' '.join(encode(message))
return phrase
除了@delimitry的回答外,还要将第二个函数的if条件中的words改为word,即改变-
if starts_with_vowel(words):
至 -
if starts_with_vowel(word):
调整这3个功能:
def starts_with_vowel(word):
# return True if the word starts with a vowel and False otherwise
return word[0] in ['a', 'e', 'i', 'o', 'u']
def encode(word):
# translate a single word to the secret language
# call starts with vowel to decide which pattern to follow
if starts_with_vowel(word):
return word[1:] + word[0] + 'ar'
else:
return word + 'way'
def translate(message):
# translate the whole text to the secret language
# call encode to translate individual words in text
return ' '.join(encode(word) for word in message)
最大的问题是 encode()
和 starts_with_vowel()
遍历所有单词(但您的评论说它应该适用于 单个单词 )