2017-10-06 105 views
0
VOWELS = ['a', 'e', 'i', 'o', 'u'] 

BEGINNING = ["th", "st", "qu", "pl", "tr"] 


def pig_latin2(word): 
    # word is a string to convert to pig-latin 
    string = word 
    string = string.lower() 
    # get first letter in string 
    test = string[0] 
    if test not in VOWELS: 
     # remove first letter from string skip index 0 
     string = string[1:] + string[0] 
     # add characters to string 
     string = string + "ay" 
    if test in VOWELS: 
     string = string + "hay" 
    print(string) 


def pig_latin(word): 
    string = word 
    transfer_word = word 
    string.lower() 
    test = string[0] + string[1] 
    if test not in BEGINNING: 
     pig_latin2(transfer_word) 

    if test in BEGINNING: 
     string = string[2:] + string[0] + string[1] + "ay" 
    print(string) 

当我取消注释下面的代码并将print(string)替换为上面两个函数中的返回字符串时,它仅适用于pig_latin()中的单词。只要将字传递给pig_latin2(),我将得到所有字都为None的值,并且程序崩溃。函数的返回值被忽略

# def start_program(): 
    # print("Would you like to convert words or sentence into pig latin?") 
    # answer = input("(y/n) >>>") 
    # print("Only have words with spaces, no punctuation marks!") 
    # word_list = "" 
    # if answer == "y": 
    # words = input("Provide words or sentence here: \n>>>") 
    # new_words = words.split() 
    # for word in new_words: 
    #  word = pig_latin(word) 
    #  word_list = word_list + " " + word 
    # print(word_list) 

    # elif answer == "n": 
    # print("Goodbye") 
    # quit() 


    # start_program() 
+0

你有没有尝试在调试模式下逐步执行代码?这应该有助于您自己查找并修复错误。 –

+0

我很努力使调试器的意义将尝试太工作了。 – Ruark

+0

如果我输入:玩石头退出,即任何将匹配我的程序工作的BEGINNINGS的单词。如果我输入:image away,即任何需要pig_latin2()的单词,我会返回None。但我已经告诉python返回字符串。 DEBUGGER:word = {NoneType}无 – Ruark

回答

0

您不捕获pig_latin2函数的返回值。所以无论那个函数做什么,你都会丢弃它的输出。

修复此线在pig_latin功能:

if test not in BEGINNING: 
    string = pig_latin2(transfer_word) # <----------- forgot 'string =' here 

当固定正是如此,它为我工作。话虽如此,还是会有一堆东西需要清理。

+0

是的。另外,'string.lower()'不影响不分配。 – Alperen