How would I pass something from a driver into a list inside a class. I then need remove a word from the list and replace it with a new word.

Pass the "driver" to a class function which appends to the list. The same for deleting from the list. You should try to keep all methods within the class.

Edited 6 Years Ago by woooee: n/a

Sorry woooee the title is misunderstanding. Just need to pass a string through does not have to be a list. Here is my first attempt. Can not be done since self.astr becomes a type error in line 16 I think.

class Sentence:

    def __init__(self,astr="I'm going back"):
        self.astr ="I'm going back"

    def setWord(self,astr=''):
        self.astr="I'm going back"

    def getfirstword(self):
        return ' Changer the word back', self.astr[2]
    def getallword(self):
        self.astr[2] = "home"
        return self.astr

def main():
    sent = Sentence()

    print sent.getfirstword()
    print sent.getallword()

main()

You are mixing strings and lists. This should help.

class Sentence:
 
    def __init__(self, astr="I'm going back"):
        self.astr = astr
 
    def setWord(self, astr):
        self.astr=astr
 
    def getfirstword(self):
        return ' Changer the word back', self.astr[2]

    def getallword(self):
        print "astr[2]", self.astr[2]
        astr_list = self.astr.split()
        print "astr_list[2]", astr_list[2]
        astr_list[2] = "XXX"
        print "join", " ".join(astr_list)
##        self.astr[2] = "home"
        return self.astr
 
sent = Sentence()
 
print sent.getfirstword()
ret_val = sent.getallword()
print type(ret_val), ret_val

Edited 6 Years Ago by woooee: n/a

Comments
Thanks again.

@wooee is keeping the problem you have in (most recent) line 10, which will return just a single character as the second part of the return duple.

The issue is whether class Sentence is more about the full sentence as a string, or more about the list of words in the sentence. Depending on the answer to this question, you will have two ways of working:

  • Sentence is a string: Constructor is as written above, there is more work done in getfirstword and getallword (see line 14 above)
  • Sentence is a list: Constructor body becomes self.astr_list = self.setword(astr) the method setword gets a body that is self.astr_list = astr.split() and the methods getfirstword and getallword can just look directly at the list without needing more work. If you need the full sentence you just use ' '.join(self.astr_list which is simplistic if the original sentence had adjacent white spaces (not likely but possible).

P.S. What happens if the sentence has punctuation. For instance if the initial sentence is "We have three colors: red, blue, and chartreuse." Does the sentence have a 'word' that is "colors:" or should it be "colors"? (and similarly for each of the color names).

Edited 6 Years Ago by griswolf: punctuation?

Comments
Thank you.
This question has already been answered. Start a new discussion instead.