Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 需要帮助循环我的代码,使其重复,而不是搜索一次_Python_Loops - Fatal编程技术网

Python 需要帮助循环我的代码,使其重复,而不是搜索一次

Python 需要帮助循环我的代码,使其重复,而不是搜索一次,python,loops,Python,Loops,我不明白如何做这个循环 term = input("") file = open('file.txt') for line in file: line.strip().split('/n') if term in line: print(line) if term in line: print('Not on database, (try using caps)') file.close() (我知道这是不正确的)如果“重复”的意思是希望

我不明白如何做这个循环

term = input("")
file = open('file.txt')
for line in file:
    line.strip().split('/n')
    if term in line:
        print(line)
    if term in line:
        print('Not on database, (try using caps)')
file.close()
(我知道这是不正确的)

如果“重复”的意思是希望用户能够多次输入术语,那么只需在循环时使用

while True:
    term = input("")
    file = open('file.txt')
    for line in file:
        line.strip().split('/n')
        if term in line:
            print(line)
        if term in line:
            print('Not on database, (try using caps)')
    file.close()
我不确定您希望能够循环多少次,但这个循环将无限期地进行

如果“重复”的意思是希望用户能够多次输入术语,那么只需使用
while
循环即可

while True:
    term = input("")
    file = open('file.txt')
    for line in file:
        line.strip().split('/n')
        if term in line:
            print(line)
        if term in line:
            print('Not on database, (try using caps)')
    file.close()

我不确定您希望能够循环多少次,但这个循环将无限期地进行

我想你需要的是这个

term = input("")
# file is a python object by default, so it's better not to use it.
f = open('file.txt')
# Put each line in the file into a list
content = f.readlines()
for line in content:
    # str.strip() does not replace the original string
    # I modify it so that it is replaced.
    line = line.strip()
    if term in line:
        print(line)
    # The line below is unnecessary because it's the same condition as the previous if statement
    # if term in line:
        print('Not on database, (try using caps)')
f.close()

我想你需要的是这个

term = input("")
# file is a python object by default, so it's better not to use it.
f = open('file.txt')
# Put each line in the file into a list
content = f.readlines()
for line in content:
    # str.strip() does not replace the original string
    # I modify it so that it is replaced.
    line = line.strip()
    if term in line:
        print(line)
    # The line below is unnecessary because it's the same condition as the previous if statement
    # if term in line:
        print('Not on database, (try using caps)')
f.close()

您可以使用
打开文件,并在各行之间循环

term = input("")
with open('file.text') as f:
    for line in f.readlines():
        if term in line.strip():
            print(line)
            break #breaks for loop and exits

您可以使用
打开文件,并在各行之间循环

term = input("")
with open('file.text') as f:
    for line in f.readlines():
        if term in line.strip():
            print(line)
            break #breaks for loop and exits

请更好地描述您正在尝试做的事情。请更好地描述您正在尝试做的事情。