翻译PygLatin中的句子

时间:2016-12-30 08:29:16

标签: 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 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("")

4 个答案:

答案 0 :(得分:0)

将整个单词集接受为句子,然后在空格上分割。以下是split函数的文档。这样的事情会对你有帮助,

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

答案 1 :(得分:0)

您可以使用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("")

HTH!

答案 2 :(得分:0)

我已更新您的代码以简化它。

我添加了一个只输入一个字母的条件。我已将退出条件放在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")

答案 3 :(得分:0)

word = raw_input("输入要翻译的单词或退出:")允许您连续输入

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

需要做的事:

尝试:

        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 !')