2017-10-06 8 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) 

上記の2つの関数で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

私が入力した場合:play stone quit、つまりBEGINNINGSと一致する単語があれば私のプログラムが動作します。私がinput:image away、つまりpig_latin2()が必要な単語を入力した場合は、Noneが返されます。しかし、私はPythonに文字列を返すように指示しました。デバッガ:word = {NoneType}なし – Ruark

答えて

0

pig_latin2関数の戻り値をキャプチャしていません。関数が何であれ、出力を破棄します。

修正pig_latin機能では、この行は:thusly固定すると

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

、それは私のために動作します。それを言っても、まだ掃除のためのものがたくさんあるでしょう。

+0

はい。また、 'string.lower()'は代入なしには影響しません。 – Alperen

関連する問題