[Tutor] Looking for Words - Help

Dave Angel davea at davea.name
Sat Oct 12 13:50:44 CEST 2013


On 12/10/2013 01:18, Jackie Canales wrote:

You're still posting in html, not text.  I stripped out the markup
portion of your message, but you're still left with lots of invisible
characters in the following. Your email program is stuffing "\xa0"
characters in place of every other space, which cuts the indentation
in half.

Here's the html in your message for just one of the lines below:

<div>    infile.close()</div>

In the text version, it looks approximately like this:

_ _ infile.close()

some readers will just show it as
  infile.close()


When you compose your message in your email program, please mark it as a
text message in your email program. Otherwise you can never be sure
what each of us will see.


> from string import punctuation
> #helper function
> def strip(text):
>     'returns text with all punctuation removed'
>
>     for symb in punctuation: #import from string
>         text = text.replace(symb, ' ')
>         
>     return text
>
> #helper function
> def wordin(s,t):
>     'does word s occur in text t'
>     #s in t
>     t = strip(t)
>     return s in t.split()

I have no idea how that function will be useful.  It makes much more
sense to me to strip the entire file, then split it into a list of
words.  Then you loop through that list of words, seeing which of those
words match your sample word.

>
> def lines(name, word):
>     'print all lines of name in which word occurs'
>
>     infile = open(name, 'r')
>     lst = infile.read()
>     infile.close()
>
>     for line in lst.splitlines():
>         if word in line:
>            words = line
>     for index, word in enumerate(words):
>         print(words)
>
> LINK TO CODE:http://codetidy.com/6926/
>
> Thank you for the help so far! I figured out how to split the file in order to only out all the lines that have the word river in it. 

But that's not what the assignment called for.  You shouldn't be
splitting on lines, but on words.  Otherwise, how are you going to
handle a line that starts with the word river?  Make a single list
containing all the words in the file.


> I tried reading
> up on enumerate but I am still having trouble understanding it. The
> code above just repeats this line in the middle of the page about 20
> times.
>
>     probably essentially dam the widest river in the world.

Well, it prints the same thing because you're printing "words", rather
than "word."  And of course if you wanted to actually see the index
working, you'd have to print that as well.

for index, word in enumerate(words):
    print(index, word)

>
> LINK: http://imgur.com/7pZvfzA
>
> I tried understanding the feedback given to me the above is what i got so far if you could please help me with the enumerate.
>
> Thanks
>


-- 
DaveA




More information about the Tutor mailing list