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 def getallword(self): self.astr = "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 def getallword(self): print "astr", self.astr astr_list = self.astr.split() print "astr_list", astr_list astr_list = "XXX" print "join", " ".join(astr_list) ## self.astr = "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
@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:
getallword(see line 14 above)
self.astr_list = self.setword(astr)the method
setwordgets a body that is
self.astr_list = astr.split()and the methods
getallwordcan just look directly at the list without needing more work. If you need the full sentence you just use
' '.join(self.astr_listwhich 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?
Hi I'm having a problem implementing a mini shopping cart drop down in the header to show the user all the products they have in their shopping cart. It seems the only solution for this is Ajax, and I've looked all over and can't find anything that I could possibly ...
Hello All ...
Iam Getting An Error With try to excecute the stored procedure .
I have Have Sql database , the stored procedure like so :
USE [MPRS] GO /****** Object: StoredProcedure [dbo].[Search_Licenses_By_Number] Script Date: 26-Nov-16 8:06:52 AM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE ...
Help! I want to create a java program that finds the highest even integer among the values entered by the user. Stop asking values when a value less than 1 have been entered. If no even integer is entered, display "No Even Integer"
Here is the sample output that I ...