【問題文】
英語の文章からなる文字列 str_engsentence が引数として与えられたとき、str_engsentence 中に含まれる3文字以上の全ての英単語を要素とするリストを返す関数 collect_engwords を作成して下さい。ただし、同じ単語を要素として含んでいて構いません。
【間違い】
def
1 list_punctuation = [".", ",", ":", ";", "!", "?"] 2 for j in range (len(list_punctuation)): 3 str_engsentences = str_engsentences.replace(list_punctuation[j],"") 4 list1 = str_engsentences.split(" ") 5 list2 = [] 6 for j in range(len(list1)): 7 if len(list1[j])>= 3: 8 list2.append(list1[j]) 9 return list2 10コード
【正解】
def
1 list_punctuation = [".", ",", ":", ";", "!", "?"] 2 for j in range (len(list_punctuation)): 3 str_engsentences = str_engsentences.replace(list_punctuation[j],"") 4 list1 = str_engsentences.split(" ") 5 list2 = [] 6 for j in range(len(list1)): 7 if len(list1[j])>= 3: 8 list2.append(list1[j]) 9 return list2 10コード
まだpythonを初めて3日目の初心者です。
おそらく、インデントの持つ意味に混乱しているのだと思います。
皆様のお知恵をお貸しくださいませんでしょうか?
インデントが潰れて読めないので、次の画像に従ってコードを貼りなおしてください。https://teratail.storage.googleapis.com/uploads/contributed_images/56957fe805d9d7befa7dba6a98676d2b.gif
問題の出典を示した方が良いです(web上ならリンク、書籍等は書籍情報や出版社のページへのリンク、学校の課題や自作問題等ならその旨明記。また、問題作成者の定めた規約等で外部への公開などが制限されている場合もあるので、確認してください)
インデントが問題だと質問文で認識しているのに、コードのインデントが反映されていないことに無頓着なのはいかがでしょう…