[Tutor] Shashwat Anand you helped with search

Alan Gauld alan.gauld at btinternet.com
Tue Feb 2 20:07:50 CET 2010


"jim serson" <fubarninja at hotmail.com> wrote

> I think I am using the right method in the wrong way. 

I'm not sure what you expect this to do...

> If you could tell me if I am trying the correct method or give 
> me a push in the right direction that would be grate thanks.

Maybe a push...
 
> look_in = raw_input ("Enter the search file to look in ")
> search = raw_input ("Enter your search item ")

OK, so far. You want to look for search in the file.
 
> c = open(look_in, "r").read().count(search.split()

Assuming there should be another closing paren...
this opens the file and reads it into memory. 
It then counts the occurences of a list produced 
by splitting the search string. This gives a Typeerror for me!
Is that what you wanted?
Or do you actually want to search for each item in your 
search string? I think you are trying to cram too much 
into one line - if for no other eason that it makes debugging 
almost impossible.

Try this:

txt = open(look_in).read()
search2 = search.split()

print search2    # is this what you expect?
    
for item in search2:
     c = txt.count(item)
     print item, txt.count(c)    # is this what you expect?
     if c > 0:   print search, ",", c,"Of your search was found"
     else:   print item, "was not found"
       
It might give you clues as to what you really want.
But using multiple lines is usually a good idea, 
especially when trying to get it working. Its much easier to 
see where things are breaking that way...


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list