2016-12-30 58 views
0

我能夠翻譯,但它只翻譯一個詞。我真的不明白如何使用多個詞翻譯一個句子。在PygLatin中翻譯句子

希望有人能幫助我!

#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("") 
+0

ÿ你應該看看分割函數,它用分隔符分割一個句子。 –

+0

你的代碼並不處理只輸入一個字符的情況,比如'a' –

回答

0

接受整組單詞作爲句子的,然後在空白分裂。這裏是關於split函數的文檔。像這樣的東西會幫助你,

sentence = raw_input("Enter your sentence ") 
words = sentence.split() 
for word in words: 
    # Execute your pyglatin code on `word` 
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!

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") 
+0

你將如何在分割函數之後重新加入輸出字符串? – Francesco

+0

那麼,你需要將所有用命令「print」打印的字符串存儲在列表中。 ('stored_strings = []','stored_strings.append('word')')。然後你可以使用'''.join(stored_strings)'或''n'.join(stored_strings)' –

+0

我嘗試追加和連接字符串,但沒有結果。無可否認,我只是一個初學者。我更新了我的代碼,以便您可以看到我到目前爲止的內容。謝謝! – Francesco

0

字=的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 !') 
+0

你應該清理你的答案。 –