November 9th, 2004, 06:08 PM
Need help on PythoN please! :)
I need some help once more, thank you.
This time I want to do a "Searching" code.
Here is an FILE called "Ordlista.txt" with 1862 Words that contains only 5 letters.Click here to see Ordlista
I want to make a code, i.e. create a "kolla1.py" that will at the start of the Terminalconsole will get a word from the user. Then the program will ask the user what list to check (Ordlista) and he will have to write in ordlista.txt
The program shall then find the word the user asked about from the beginning it shall seem something like this.
:~> python Kolla1.py trams
Write name of the file to search in: ordlista.txt
The word trams is on the list.
Can anyone help me out? About how i shall begin ? :S
November 9th, 2004, 06:54 PM
First you need to use sys.argv to get the wor dto be searched for, that the user inputs in the command line as the argument to your .py file. Then you can use raw_input() to get the name of the file. You open that file, read ines, nd then you can create a list or an array of strings of all words in the file. Then you can say, if the word he user input is in the list or the array, rint word found, else print word not found.
It'd go something like that... One could do a better job, but this is just from at the top of my head right now...
November 10th, 2004, 05:11 AM
I know what you mean this is what i got so far but it doesn't work since I haven't completed it, but I dont know what I should do now? Need help please.
from string import *
a=raw_input("Skriv in namnet på filen: ")
for n in text:
November 11th, 2004, 01:28 PM
There is a builtin function called "list", and with that code you are overwriting it, which isn't good because it can cause problems elsewhere in the program...
An open file has the "readlines()" method to get all the lines in one go and return a list to save you building the list yourself.
for n in text:
Lastly, one of the great if tests in Python is:
if item in sequence:
and it will search the list/string/tuple/etc for you, so what you could do is something like this:
word = raw_input("Please enter a word: ")
filename = raw_input("Please enter the file to search: ")
data = open(filename).readlines()
if word in data:
print "Word found!"
print "word not found :("