Hi, this is my first attempt at Python, what I am trying to achieve is a script that will ask the user for input, search a txt file and display the output or give not found message. Ideally I would like it to ask the user once the first output is achieved for further user input if required, otherwise exit the program. However, would be happy if it achieved the first objective.
Following is as far as I can go, the problem is it is breaking after the first find, whereabouts I know there are additional matches in the txt file. I think I might need to replace the IF with a WHILE, which I have tried but keep getting errors, and am unable to work it out. - print"\nSEARCHING NUMBERS"
-
-
print "\nPlease Enter the Number 1, 2, 3, 4 or 5."
-
-
text_file = open("read_it.txt", "r")
-
-
word = raw_input("Type the Number you want to check: ") word = word.upper()
-
-
print "\nnumber."
-
for lines in text_file:
-
-
# if the line does not contain the typed number
-
# then continue to the next line
-
-
if ''.join(lines).find(word) == -1:continue
-
print lines
-
-
break
-
else:
-
-
print "Not found" # Executed whenever break above is NOT executed
-
print raw_input("\n\nPress the enter key to exit.")
-
-
text_file.close()
I am only a beginner, so would appreciate any help or suggestions thank you.
L :)
4 2136 boxfish 469
Recognized Expert Contributor -
# if the line does not contain the typed number
-
# then continue to the next line
-
if ''.join(lines).find(word) == -1:continue
-
print lines
-
break
-
else:
-
print "Not found" # Executed whenever break above is NOT executed
-
This code is confused. I don't know which code is supposed to be in the if block. A continue jumps directly up to the top of the loop. There's no point in doing anything like breaking or printing anything after you have used continue. For now, you don't have to use continue or break; a simple if-else should do it. -
if the word is found:
-
print "Found"
-
else:
-
print "Not found"
-
As for checking if the word is found, the find function is not nessecary. just use the in operator: -
if word in lines:
-
print "Found"
-
Hope this helps.
thanks Boxfish, I am continuously getting an "invalid syntax"
.
I appreciate your feedback and can see the confusion, (it's a true reflection of the writer).
L :(
boxfish 469
Recognized Expert Contributor
Sorry, that was pseudocode. Use the second bit of code that uses the in operator to find the word.
The more you program, the less confused you will be.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Les Juby |
last post by:
A year or two back I needed a search script to scan thru HTML files
on a client site. Usual sorta thing. A quick search turned up a
neat script that provided great search results. It was fast,
returned the hyperlinked page title, filename, and the body txt (30
preceding and following words) in context with the search word
highlighted. Excellent.!
See it working at: http://www.ipt.co.za
Just search for "firearm"
|
by: Steph |
last post by:
Hi.
I'm very new to MS Access and have been presented with an Access database
of contacts by my employer.
I am trying to redesign the main form of the database so that a button
entitled 'search' may be clicked on by the user and the user can then
search all records by postcode. I want to do this to prevent duplicate
data entry.
|
by: Robert |
last post by:
Does anybody have the code for, an example of, or a link to a
text-search function that supports exact phrase, any word, and all words
searching in an Access table. If so, could you please post it?
Robert
This is what I need:
I need to be able to search on a text field in a table. I want to give
the user the opportunity to search on
|
by: Tarique Jawed |
last post by:
Alright I needed some help regarding a removal of a binary search
tree. Yes its for a class, and yes I have tried working on it on my
own, so no patronizing please. I have most of the code working, even
the removal, I just don't know how to keep track of the parent, so
that I can set its child to the child of the node to be removed.
IE - if I had
C
/ \
B D
|
by: zakhirn |
last post by:
Hello,
I have an extremely new user to XML and XSL, and I would like to know
how to search XML data via a input form in HTML, and have the results
displayed in HTML.
If anyone has code that can do that, could you please post the relevant
files. I have searched the internet for a while now, and have been
completely unsuccessful. I would like to do it using javascript in my
XSL file if possible, or using CGI script to search the XML...
| |
by: fjm |
last post by:
Hello all,
I have 2 files. index.php which is a log in page and a search page. After the user logs in they are taken to the search page by way of a checklogin script that verifies the user.
My index page has two fields:
customer
password
Here are the contents of both of my files. I'm sure I am missing something here but 4 hours later and I am stll in the same place.
|
by: smartic |
last post by:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Search</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="index.php">
<input name="Search_IN" type="text" id="Search_IN" maxlength="44" />
<input type="submit" name="SearchBTN" id="SearchBTN" value="Search" />
|
by: Merlin1857 |
last post by:
How to search multiple fields using ASP
A major issue for me when I first started writing in VB Script was constructing the ability to search a table using multiple field input from a form and having the sql statement dynamically built according to the input provided by the user. I have used the method described here hundreds of times it is quick and adaptive. I generally use a frames page for the search, in this way the search is maintained...
|
by: phl |
last post by:
hello,
I am trying to explore options on the various types of search engines
for searching user input. User input maybe stored in XML files. Does
anyone know if there are any good ready made search engines which will
automatically index the user input from the XML file(s), so I can
create a search functionality based on keywords.
I am aware that a DB solution is the obvious route. The purpose here
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |