gpt4 book ai didi

python - python 中的基本英语到 Pig Latin 翻译

转载 作者:太空宇宙 更新时间:2023-11-03 20:00:20 25 4
gpt4 key购买 nike

我想制作一个 Pig Latin 翻译器。

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)

最佳答案

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

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

关于python - python 中的基本英语到 Pig Latin 翻译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59278567/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com