この質問は私の教育に関連しています。それは、あなたが私にできる限り詳細な情報を提供してくれることを望んでいるということです-コードをコピーして貼り付けて渡したくありません.:)
タスクは簡単です。writeshort(txt) という定義を作成し、単語の文字列を取得して、5 文字未満の単語のみを出力します。今、私はこれを完了しましたが、問題はタスクが具体的に use a definitionと言うことです。ここで失敗します。
動作する定義のないコード:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
string = raw_input(”Write a few lines: ”)
txt = string.split()
result = []
for words in txt:
if len(words) > 4:
continue
result.append(words)
print ', '.join(result), ”have less than five letters!”
これで見栄えが良くなり、厄介な [' '] なしで印刷されます。しかし、定義はどうですか?私はいくつかのことを試しましたが、これが最新ですが、5文字未満の最初の単語のみを出力し、残りは無視します:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
string = raw_input(”Write a few lines: ”)
txt = string.split()
def writeshort(txt):
for txt in txt:
if len(txt) > 4: #Yes I know its a 4, but since it counts 0...
continue
return txt
print writeshort(txt), "have fewer letters than five!"
助けていただければ幸いです。Python の学習に時間を割いていただき、ありがとうございます。