這個程序的目的是將輸入的句子翻譯成拉丁文。程序中的前兩個函數可以正常工作,但最後一個函數不會執行。當我嘗試在編譯器中執行它時,該函數只會給我重複的空白行。爲什麼總是返回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
哦,你的代碼示例似乎已經崩潰了 –
沒有堆棧溢出告訴我有一個錯誤,所以我不得不發佈它,然後編輯代碼:) –
真棒強迫工作流程,不要介意我然後:) –