python


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

Related Links

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

Categories

HOME
shopify
amazon-swf
jsviews
smartphone
label
hana
ubuntu-12.04
chaiscript
hyperledger-fabric
user
devstack
spring-cloud-contract
dropbear
lotus-notes
metronic
rfid
tee
automata
store
simple-injector
scala-native
tree-traversal
jogl
jconsole
google-ima
url.action
ui5
maximo
vcard
ggmap
data-conversion
pitest
red-black-tree
crt
canvasjs
openshift-enterprise
pmwiki
autocompletetextview
knockout-3.0
pdftk
context-free-language
vcf
shutdown
rpostgresql
fedora20
castle-dynamicproxy
mercury
redux-router
gulp-typescript
pdflib
typhoon
tomcat5
foxit
django-filer
metalsmith
inotifypropertychanged
remoteapp
date-format
elastix
search-regex
proxygen
sun-codemodel
asymptote
self-hosting
datainputstream
hornetq
ninject.web.mvc
rad
ipojo
wif
harp
subversion-edge
consensus
ctrlp
activity-streams
hidden-field
dalekjs
dto
funq
system.web
initialization-vector
pyjade
bignum
nimrod
javascriptmvc
vertical-scrolling
fluidsynth
wpdb
mozart-mvc
android-4.0
bulbs
javascriptserializer
windows-phone-7-emulator
emacs23
selectonemenu
cross-domain-policy
objectbrowser
querystringparameter
search-path
out-of-browser
clients
dirty-data

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App