我正在尝试从 python 中的文本文件中打印两行文本,它们在单独的行上彼此相邻。所以文本文件看起来像这样:
Apples
Oranges
Pears
Lemons
Run Code Online (Sandbox Code Playgroud)
如果有人输入苹果,我希望程序打印出来:
Apples
Oranges
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止的代码:
file = open('Fruit.txt',"r")
for line in file:
if InputText in line:
print(line)
print(line+1)
return
file.close()
Run Code Online (Sandbox Code Playgroud)
当然,该变量line+1不正确,但我将其留在那里是为了说明还必须打印下一行文本。