Need help on looping my code so it repeats instead of searching once
i dont under stand how to make this loop 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() (i know it is incorrect)
If by "repeat" you mean you want the user to be able to input a term multiple times, then just use a while loop. 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() I am not sure how many times you want to be able to loop, but this loop will go indefinitely.
I guess what you need is this. 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()
You can open the file using with and loop through the lines. 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
How to remove quotes and brackets from a dict that has two keys to one value in a jinja template?
I am looking at each individual date in a Pandas dataframe and adjusting one column (weight), based on condition on another column for each date
For loop in Python working, but not doing it's job
Python error 'int' object has no attribute 'penup' (python turtle)
custome user serializer giving error on is_valid() - django
Dividing a graph into 2 disconnected subgraphs
use boto3 on GAE for Python
boto command for describing an Auto Scaling Group?
get sets of index values, grouped by column year
How to use a tensorflow model extracted from a trained keras model
How to query with many tables
python-shell on linux system indentation error
How come 1 is printed instead of 0?
How to subtracting two hyperspectral image?
Getting an error: list assignment index out of range
Launching dev_appserver.py from windows powershell gives me “too few arguments” error