私は Python プログラミングの初心者であり、次の問題を何時間も解決できませんでした。4行を含むサンプルファイルがあります。ファイル行で見つかるまでにユーザーから2つの入力を取得したいのですが、ループがすべての行を反復していないようです.これはファイルの内容であり、要素はスペースで区切られています:
Google 1 2
Apple 13 17
Microsoft 22 8
Verizon 6 15
そして、これは私のコードです:
import sys
with open('manylines.txt','r') as myfile:
results = [line.split() for line in myfile]
for i in results:
print(i)
term1=input("Enter the first term:")
while term1 not in i :
print("Term not found,please try again:")
term1 = input("Enter the first term:")
term2 = input("Enter the second term:")
while term2 not in (i) :
print("Term not found,please try again:")
term2 = input("Enter the second term:")
print(i)
print(i)
sys.exit()
たとえば、Google と Microsoft を入力して、次の出力を得たいと考えています。
['Google','1','2']
['Microsoft','22','8']
2 つのリストとして。ただし、私のコードは最初の行のみを検出し、他の行は検出しません。他の行を反復しない理由を教えてください。これを修正する方法は?前もって感謝します!