Python PygLatin中的句子翻译

Python PygLatin中的句子翻译,python,Python,我能做一个翻译,但它只翻译一个词。我真的不明白怎样才能把一个句子翻译成多个单词 希望有人能帮助我 #List Operators vowels = ["a", "e", "i", "o", "u"] consonants = ["b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z"] #Instructions print ("Program

我能做一个翻译,但它只翻译一个词。我真的不明白怎样才能把一个句子翻译成多个单词

希望有人能帮助我

#List Operators
vowels = ["a", "e", "i", "o", "u"]
consonants = ["b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z"]

#Instructions
print ("Program to translate sentences to pig latin.")
print ("Avoid using symbols and punctuation.")

#Define word
word= ""
while word != "exit":
    word = input("Enter a word(s) to translate or Exit: ")
    words = word.split()
    for word in words:
        if word.isalpha():
            word = str(word)

#Translate word by checking how it starts
            first_letter = word[0]
            if len(word) == 1:
                print ("Word is only one letter!")
            else:
                if first_letter in vowels:
                    print (word + "yay")
                else:
                    second_letter = word[1]
                    if first_letter in consonants:
                        print (word[1:] + word[0] + "ay")
                    else:
                        print (word[2:] + word[:2] + "ay")

        else:
            if word.lower() == "exit":
                word = word.lower()
                print("Goodbye.")
            else:
                print("Invalid word: %s! Try again." % word)

    stored_strings = []
    stored_strings.append('word')
    ''.join(stored_strings)
    print("")

将整组单词作为一个句子,然后按空格分开。以下是有关该函数的文档。像这样的东西会对你有帮助

sentence = raw_input("Enter your sentence ")
words = sentence.split()
for word in words:
    # Execute your pyglatin code on `word`

您只需使用string.split()函数将句子分成一个单词数组,然后迭代所有单词

例如,在代码段中添加两行将得到所需的结果

# List Operators
vowels = ["a", "e", "i", "o", "u"]
consonants = ["b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z"]

# Instructions
print("Program to translate sentences to pig latin.")
print("Avoid using symbols and punctuation.")

# Define word
word = ""
while word != "exit":
    word = input("Enter a word(s) to translate or Exit: ")
    words = word.split()
    for word in words:
        if word.isalpha():
            word = str(word)

            # Translate word by checking how it starts
            first_letter = word[0]
            second_letter = word[1]
            if first_letter in vowels:
                print(word + "yay")
            else:
                if first_letter and second_letter in consonants:
                    print(word[2:] + word[:2] + "ay", end=" ")
                else:
                    print(word[1:] + word[0] + "ay", end=" ")

        else:
            if word.lower() == "exit":
                word = word.lower()
                print("Goodbye.")
            else:
                print("Invalid word: %s! Try again.\n" % word)
    print("")

我已经更新了您的代码,使其简化了一点

我添加了一个条件,其中一个只输入一个字母。我已将退出条件放置在
while
循环的开头

我无法检查算法的真正功能,因为我没有规范

# List Operators
vowels = "aeiou"
consonants = "bcdfghjklmnpqrstvwxyz"

# Instructions
print ("Program to translate sentences to pig latin.")
print ("Avoid using symbols and punctuation.")

# Infinite loop that will be stopped when user inputs `exit`
while True:
    words = input("Enter a word or a sentence to translate or Exit: ").lower()
    if words == "exit":
        print("Goodbye.")
        break
    for word in words.split():
        # Translate word by checking how it starts
        first_letter = word[0]
        if first_letter in vowels:
            print (word + "yay\n")
        elif len(first_letter) == 1:
            print (word + "\n")
        else:
            second_letter = word[1]
            if first_letter in consonants and second_letter in consonants:
                print (word[2:] + word[:2] + "ay\n")
            else:
                print (word[1:] + word[0] + "ay\n")
word=原始输入(“输入要翻译的单词或退出:”)允许您连续输入

但在代码中键入字母可能会导致错误

需要做:

# 尝试:

第一个字母=单词[0]
第二个字母=单词[1]
如果元音中的第一个字母:
打印(word+“是的”\n)
其他:
如果辅音中的第一个字母和第二个字母:
打印(word[2:][word[:2]+“ay\n”)
其他:
打印(word[1::][word[0]+“ay\n”)
除索引器外:
打印('Input word len<2!')

你应该看看split函数,它用分隔符分隔一个句子。你的代码不处理只输入一个字符的情况,比如
a
你应该清理你的答案。在split函数之后,你如何重新连接输出字符串,例如,您需要将使用命令
print
打印的所有字符串存储在列表中。(
storaged_strings=[]
storaged_strings.append('word')
)。然后您可以使用
'.join(存储的\u字符串)
'\n.join(存储的\u字符串)
我尝试附加和连接字符串,但没有结果。诚然,我只是个初学者。我更新了我的代码,以便您可以看到我到目前为止拥有的内容。谢谢您需要将所有打印实例
print(xyz)
替换为
存储的\u字符串。追加(xyz)
,然后应用连接函数(
print('\n'.join(存储的\u字符串))
)。顺便说一句,如果你想得到帮助,你应该投票决定答案。
        first_letter = word[0]

        second_letter = word[1]

        if first_letter in vowels:

            print (word + "yay\n")

        else:

            if first_letter and second_letter in consonants:

                print (word[2:] + word[:2] + "ay\n")

            else:

                print (word[1:] + word[0] + "ay\n")

    except IndexError:

       print('Input word len < 2 !')