Python中基本的英语到Pig Latin转换

2024-09-26 22:52:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我想做一个猪拉丁翻译。你知道吗

translate_sentence(" No shirts, no shoes, no service")应该产生:

Onay irtsshay, onay oesshay, onay ervicesay

这是我的密码:

sentence = sentence.split()
for item in range(len(sentence)):
    if sentence[item][0] in "aeiou":
        sentence[item] += 'yay'
    else:
        sentence[item]=sentence[item][1:]+sentence[item][0]
        sentence[item]+='ay'
sentence = ' '.join(sentence)

print(sentence)

Tags: noin密码serviceitemsentencetranslateshirts
1条回答
网友
1楼 · 发布于 2024-09-26 22:52:55

很酷的挑战,以下是我的解决方案:

sentence = "hello my name is Ari"

# split words into a list
tokens = sentence.split(" ")

# Iterate over the list of words
for word in tokens:

    # Store the words position in the list for later
    word_index = tokens.index(word)

    # If the first character is a vowel, we'll do this
    if word[0].lower() in ('a', 'e', 'i', 'o', 'u'):
        tokens[word_index] = word + "yay"

    # If it's not we're going to do this...
    else:
        tokens[word_index] = word[1:] + word[0] + "ay"

print(" ".join(tokens))

输出:

ellohay ymay amenay isyay Ariyay

相关问题 更多 >

    热门问题