函数返回空行的问题Python

时间:2015-11-04 09:38:54

标签: python function

该计划的目的是将输入句子翻译成猪拉丁语。程序中的前两个函数工作正常,但最后一个函数根本不执行。当我尝试在编译器中执行它时,该函数只会给我重复的空行。有关为什么总是为pigLatinTranslator函数返回空白行的想法?

"""
    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

2 个答案:

答案 0 :(得分: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()

答案 1 :(得分: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() + '.'

将字符串的第一个字符大写。