Python Forum
problem nin home work - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: problem nin home work (/thread-3071.html)



problem nin home work - maayan11 - Apr-27-2017

hello,
in my home work I need to write a function that gets text file and work
and have to find the first time that the word appears and retuen (a,b)
whereas a = number of line b= nuber of word in line.
in addition if theres any functuations they need to be deleted and if somone serach for the word key and it exixt with uppercase they still could find it. ive tried to do it but doesnt work it returns me none:(
ive wrote:
def finds(word,filename)
    count=0
    word=word.lower()
    f=open(filename,'r')
    k=f.readlines()
    for j in range(len(k)):
        for t in range(len(k[j].split())):
              if k[j].split()[t].isdigit()==False or k[j].split()[t].isalpha()==False:
                    k[j].split()[t]=k[j].split()[t].lower().rstrip['!@,./;']     
                    if word==k[j].split()[t]:
                        b=j+1
                        count=t+1
                        return (count,b)
thanks!


RE: problem nin home work - nilamo - Apr-27-2017

Try simplifying it so you can test what you're doing.  For starters, get rid of the file completely and just work with a string.  Once that works, then you just make a small change to use a file instead.

text = '''
Hello darkness, my old friend
I've come to talk with you again
Because a vision softly creeping
Left its seeds while I WAS sleeping
And the vision that was planted in my brain
Still remains
Within the sound of silence
'''.split("\n")

look_for = "was"

#enumerate over the lines
   #enumerate over the words
       #check for match, and print if found