更新:私の現在の質問は、新しい検索フレーズごとに最初からコードをEOFに読み取るにはどうすればよいかということです。
これは私が行っている課題であり、現在は固執しています。これはPythonを使用した初心者向けのプログラミングクラスです。
jargon = open("jargonFile.txt","r")
searchPhrase = raw_input("Enter the search phrase: ")
while searchPhrase != "":
result = jargon.readline().find(searchPhrase)
if result == -1:
print "Cannot find this term."
else:
print result
searchPhrase = raw_input("Enter the search phrase: ")
jargon.close()
割り当ては、ユーザーのsearchPhraseを取得し、ファイル(jargonFile.txt)で検索して、結果(発生した行と文字の発生)を出力することです。オカレンスの行番号を見つけるためにカウンターを使用しますが、これは後で実装します。今のところ私の質問は私が得ているエラーです。ファイル全体を検索する方法が見つかりません。
サンプル実行:
Enter the search phrase: dog
16
Enter the search phrase: hack
Cannot find this term.
Enter the search phrase:
「dog」は最初の行にありますが、jargonFileの他の行にもあります(文字列として複数回)が、最初の行の最初の出現のみを示しています。文字列ハックはjargonFileで何度も見つかりますが、私のコードは最初の行のみを検索するように設定されています。この問題を解決するにはどうすればよいですか?
これが十分に明確でない場合は、必要に応じて課題を投稿できます。