#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Sweden
    Posts
    20
    Rep Power
    0

    Need help on PythoN please! :)


    Hello folks!

    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
    Wordlist read!
    The word trams is on the list.

    Can anyone help me out? About how i shall begin ? :S

    Thanks,
  2. #2
  3. A wanna-be guru of some sort
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2004
    Location
    Either online or offline
    Posts
    625
    Rep Power
    20
    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...
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Sweden
    Posts
    20
    Rep Power
    0
    Hey dude,

    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.

    Code:
    #coding: iso-8859-1
    from string import *
    a=raw_input("Skriv in namnet på filen: ")
    text=open(a)
    list=[]
    for n in text:
        list=list.append(n)
    print list
    text.close()
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    Code:
    list=[]
    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...

    Code:
    for n in text:
        list=list.append(n)
    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.

    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:

    Code:
    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!"
    else:
        print "word not found :("

IMN logo majestic logo threadwatch logo seochat tools logo