0

这是 codeacademy 中使用的标准猪拉丁代码。它工作得很好,但它的缺点是它一次只能用于一个单词:

pyg = 'ay'

original = raw_input('Enter a word or phrase:')

if len(original) > 0 and original.isalpha():
    word = original.lower()
    translate = word[1:] + word[0]
    if word[0] != "a" and word[0] != "e" and word[0] != "i" and word[0] != "o" and word[0] != "u":
        new_word = translate + pyg
        print new_word
    else:
        new_word = word + pyg
        print new_word
else:
    print 'Input is empty or illegal'

所以我想做它以便它可以短语。这就是我想出的:

pyg = 'ay'

count = 0

original_input = raw_input('Enter a word or phrase:')

original = original_input

original_list = []

#converts to a list
while " " in original:
    if count > 50:
        break
    word = original[0:original.index(" ")]
    original_list.append(word)
    space = original.index(" ")
    space += 1
    original = original[space:]
    count += 1
#this works great until there is a word left and no spaces i.e. the last word
if len(original) > 0:
    original_list.append(original)
#this adds the last word

print original_list

def pyglatin(phrase):
#old code doesn't work because phrase is a list
        #now I have to translate BACK to a string
        for words in phrase:
            new_word = str(words)
    """this works for one word, how do I assign a new variable for every word if I don't know the phrase length ahead of time"""

所以这让我想到了我的问题:当我不知道我需要多少项目时,如何为每个项目分配一个变量,然后能够调用该代码(通过旧的 pyglatin 翻译器)?

4

2 回答 2

0

正如 Maxime 所提到的,列表在这种情况下很有用,并且可以更容易地将字符串转换为列表。为了解释短语,我将代码重写如下:

def trans_word(original):
    if len(original) > 0 and original.isalpha():
        word = original.lower()
        translate = word[1:] + word[0]
        if word[0] != "a" and word[0] != "e" and word[0] != "i" and word[0] != "o" and word[0] != "u":
            new_word = translate + pyg
            return new_word
        else:
            new_word = word + pyg
            return new_word
    else:
        return "Input is empty or illegal"


def pyglatin(phrase):
    if len(phrase) > 0:
        return " ".join(trans_word(word) for word in phrase)
    else:
        return "Input is empty or illegal"


pyg = 'ay'
count = 0
original_input = raw_input('Enter a word or phrase:')
original_list = original_input.split()

print pyglatin(original_list)
于 2013-11-12T13:31:17.963 回答
0

也许是这样的?

def latinize(word):
        word = word.lower()
        translate = word[1:] + word[0]
        return (translate if word[0] in 'aeiou' else word) + 'ay'

original = raw_input('Enter a word or phrase:')
print ' '.join(latinize(word) for word in original.split())

示例输入和输出:

Enter a word or phrase:Gallia est omnis divisa in partes tres
galliaay steay mnisoay divisaay niay partesay tresay

但是你不应该把所有的声母都移到末尾,而不仅仅是第一个吗?

编辑:

如果要将所有声母移到末尾,可以使用:

def latinize(word):
        word = word.lower()
        firstvowel = min (word.index (v) if v in word else 42042 for v in 'aeiou')
        if firstvowel == 42042: raise Exception ('No vowel in word')
        return word [firstvowel:] + word [:firstvowel] + 'ay'

original = raw_input('Enter a word or phrase:')
print ' '.join(latinize (word) for word in original.split () )

样本输入和输出:

Enter a word or phrase:star chick mess string
arstay ickchay essmay ingstray
于 2013-11-12T13:30:22.613 回答