2015-11-04 93 views
1

這個程序的目的是將輸入的句子翻譯成拉丁文。程序中的前兩個函數可以正常工作,但最後一個函數不會執行。當我嘗試在編譯器中執行它時,該函數只會給我重複的空白行。爲什麼總是返回pigLatinTranslator函數的空白行?函數返回空行的麻煩Python

""" 
    File: pa07.py 
    Author: Casey Gilles 
    Description: Converts a single sentance into pig latin. 
    Deadline: November 4, 2015. 
""" 

#function findFirstVowel 
#Inputs: word from the input start_sentance 
#Outputs: Index of first vowel found. If no vowels are found, -1 is #returned. 
#Description: Returns the index of first vowel found in a word. If no vowels returns -1. 

def findFirstVowel(word): 
    vowels = "aeiou" 
    for char in word: 
     if char in vowels or char in vowels.upper(): 
      index = word.find(char) 
      return index 
    else: 
     return -1 

""" 
    function translateWord 
    Inputs: Index from the word used in the findFirstVowel function and the word itself. 
    Outpus: The input translated into piglatin 
    Description: Takes in an index and a word as inputs. Determines which rule to follow 
      and based on that rule modifies the word in a certain way 
""" 

def translateWord(word): 
    index = findFirstVowel(word) 
    if index == -1: 
     translated_word = word 
    elif word[0] in "aeiou" or word[0] in "AEIOU": 
     translated_word = word + "way" 
    else: 
     translated_word = word.replace(word[:index],'') + word[:index]+"ay" 
    return translated_word 

""" 
function pigLatinTranslator 
Inputs: Sentance issued by the user to be translated. 
Outputs: Original sentance translated into pag latin but with the same  formatting. 
Description: This function takes in a sentance and translates the sentance into pig latin. 
""" 

def pigLatinTranslator(input_sentance): 
    count = 0 
    input_sentance = input_sentance.replace('.','') 
    sentance_list = input_sentance.split(' ') 
    for item in sentance_list: 
     new_word = translateWord(item) 
     sentance_list.insert(count,new_word) 
     count = count + 1 
    temp_sent = ' '.join(sentance_list) 
    final_translated_sentance = temp_sent[0].upper()+temp_sent[1:].lower()+'.' 
    return final_tranlated_sentance 

回答

0

首先在你的第一個功能else混淆了我一下。我寧願使用:

def findFirstVowel(word): 
    vowels = "aeiou" 
    for char in word: 
     if char in vowels or char in vowels.upper(): 
      index = word.find(char) 
      return index   

    return -1   #only if all characters have been searched 

然後在pigLatinTranslator我認爲這個問題是「對每個」 -loop名單及將物品進入該名單上的結合。
你可以使用,而不是第二個列表:

def pigLatinTranslator(input_sentence): 
    #count = 0 
    input_sentence = input_sentence.replace(".","") 

    sentence_list = input_sentence.split(" ") 

    res_list = []  #The list for the results 

    for item in sentence_list: 
     new_word = translateWord(item) 
     #sentence_list.insert(count,new_word) 

     res_list.append(new_word) 
     #count = count + 1 

    #temp_sent = " ".join(sentence_list) 
    temp_sent = " ".join(res_list) 
    final_translated_sentence = temp_sent[0].upper()+temp_sent[1:].lower()+"." 
    return final_translated_sentence 

或者你可能只寫:

return temp_sent[0].upper()+temp_sent[1:].lower() 
+0

哦,你的代碼示例似乎已經崩潰了 –

+0

沒有堆棧溢出告訴我有一個錯誤,所以我不得不發佈它,然後編輯代碼:) –

+0

真棒強迫工作流程,不要介意我然後:) –

0

你必須在代碼的一些縮進錯誤,但我以爲他們發生了,當你發佈的代碼這裏。順便說一句,我糾正了你的「句子」的拼寫 - 我發現「感知」有點分散注意力。 :)

pigLatinTranslator中的問題是,您正在嘗試修改sentence_list而您正在迭代它。這通常不會如你所期望的那樣工作。簡單地創建一個新列表要好得多。

這是你的代碼的工作版本:

""" 
    File: pa07.py 
    Author: Casey Gilles 
    Description: Converts a single sentence into pig latin. 
Deadline: November 4, 2015. 
""" 

def findFirstVowel(word): 
    """ 
    function findFirstVowel 
    Inputs: word from the input start_sentence 
    Outputs: Index of first vowel found. If no vowels are found, -1 is #returned. 
    Description: Returns the index of first vowel found in a word. If no vowels returns -1. 
    """ 

    vowels = "aeiou" 
    for char in word: 
     if char in vowels or char in vowels.upper(): 
      index = word.find(char) 
      return index 
    else: 
     return -1 

def translateWord(word): 
    """ 
    function translateWord 
    Inputs: Index from the word used in the findFirstVowel function and the word itself. 
    Outputs: The input translated into piglatin 
    Description: Takes in an index and a word as inputs. Determines which rule to follow 
      and based on that rule modifies the word in a certain way 
    """ 

    index = findFirstVowel(word) 
    if index == -1: 
     translated_word = word 
    elif word[0] in "aeiou" or word[0] in "AEIOU": 
     translated_word = word + "way" 
    else: 
     translated_word = word.replace(word[:index],'') + word[:index]+"ay" 
    return translated_word 


def pigLatinTranslator(input_sentence): 
    """ 
    function pigLatinTranslator 
    Inputs: Sentance issued by the user to be translated. 
    Outputs: Original sentence translated into pag latin but with the same  formatting. 
    Description: This function takes in a sentence and translates the sentence into pig latin. 
    """ 

    input_sentence = input_sentence.replace('.','') 
    sentence_list = input_sentence.split(' ') 
    new_sentence_list = [] 
    for item in sentence_list: 
     new_word = translateWord(item) 
     new_sentence_list.append(new_word) 
    temp_sent = ' '.join(new_sentence_list) 
    final_translated_sentence = temp_sent[0].upper()+temp_sent[1:].lower()+'.' 
    return final_translated_sentence 


test = 'This is a test sentence.' 
output = pigLatinTranslator(test) 
print(output) 

輸出

Isthay isway away estay entencesay. 

有你可以做一些改進。例如,而不是

if char in vowels or char in vowels.upper(): 

,你可以簡單地做

if char.lower() in vowels: 

另一個簡化是

final_translated_sentence = temp_sent.capitalize() + '.' 

大寫字符串的第一個字符。