我正在创建一个简单的猪拉丁语翻译。以下是我目前所掌握的情况:
while True:
phrase = input('Translate > ').lower().split()
for word in phrase:
if word[0] in 'aeiou': # If the first letter is a vowel
print(word + '-way') # Add suffix 'way'
else:
c = # Number of consonants before the first vowel
print (word[c:] + word[0:c] + '-ay')
如何使c
尽可能简单地等于word
中第一个元音之前的辅音数目?我不想使用函数。在
我不想定义我的函数。很抱歉。在
使用itertools.takewhile
takewhile得到一个
predicate
,这就是这里的lambda,当谓词是True
时,它将使用元素,因此在这种情况下,只要遇到一个元音,该方法就会停止并返回到该点为止的辅音列表,我们只需使用len
函数来检查列表中有多少辅音,为我们提供c
的索引。在相关问题 更多 >
编程相关推荐