Python Forum
problem nin home work
Thread Rating:
  • 2 Vote(s) - 4.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem nin home work
#1
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!
Reply
#2
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  home work rolling average arcticfox286 1 30,359 Mar-02-2019, 04:55 AM
Last Post: ichabod801
  Could someone please help walk me through my home work tetoronga 3 2,949 Mar-22-2018, 09:33 PM
Last Post: nilamo
  a Home language TDH 4 3,403 Oct-17-2017, 03:30 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020