【解決方法】この関数が pyg-latin 値を返さないのはなぜですか?


私のコード:

#Problem: Move the first letter of each word to the end of it, then add #"ay" to the end of the word. Leave punctuation marks untouched.

#Example: "The train is fast !" --> "heTay raintay siay astfay !"

def pig_it(text):
    import string
    words = text.split()
    for word in words:
        first_letter = word[0]
        word += first_letter
        word = word[1:]
        if word not in string.punctuation:
            word += ("ay")
    new_words = ((" ").join(words))
    return(new_words)


pyg_latin = pig_it("The train is fast !")
print(pyg_latin)

出力は「電車は速いです!」を返します。 ピグラテン値を返す必要がある場合。

私が試したこと:

デバッグを試してみたところ、いくつかの結果が得られました。 for ループ中は、各単語 (要素) がそれぞれの pyg-latin 値に正しく変更されているように見えますが、for ループの後、単語リストは元の値に戻されます。

解決策 1

Word を変更していますが、変更を保存していません。 試す

<pre lang="Python">
import string

def pig_it(text):
    pig = []
    for word in text.split():
        if word not in string.punctuation:
            pig.append(word[1:] + word[0] + 'ay')

    return ' '.join(pig)


pyg_latin = pig_it("The train is fast !")
print(pyg_latin)

コメント

タイトルとURLをコピーしました