2

I am working on a problem set for a Python class. We are being introduced to Classes. I am trying to (A) create a class called Sentence with a single parameter, string, and to create an instance variable that stores the sentence as a string. (B) Then to assign accessor methods for the class: getSentence(return the sentence as a string), getWords(return the list of words in the sentence), getLength(return the number of characters in the sentence), and getNumWords(return the number of words in the sentence). Below is what I have attempted thus far:

line = "This Tuesday is Election Day"

class Sentence:
    def __init__(self, text, string, words, length, num):
        self.text = sentence
        self.string = str(self.text)
        self.words = self.text.split()
        self.length = len(self.text)
        self.num = len(self.words)

    def getSentence(self):
        return self.string

    def getWords(self):
        return self.words

    def getLength(self):
        return self.length

    def getNumWords(self):
        return self.num



line.getWords()

Thank you for your time.

Below is the updated code that works:

class Sentence:
    def __init__(self, string):
        self.astring = str(string)

    def getSentence(self):
        return self.astring

    def getWords(self):
        return (self.astring).split()

    def getLength(self):
        return len(self.astring)

    def getNumWords(self):
        return len(self.getWords())



string = Sentence("The Election is tomorrow")

print (string.getSentence())
print (string.getWords())
print (string.getLength())
print (string.getNumWords())
3
  • Did it work? What is the problem? Commented Nov 6, 2016 at 19:09
  • It doesn't work how I imagine it. I create an example string above, "This Tuesday..." and when I input line.getSentence() I receive "AttributeError: 'str' object has no attribute 'getSentence'" Commented Nov 6, 2016 at 19:11
  • 1
    That's your first problem. You want to pass the string to the class for further processing. A simple test would be foo = Sentence(line). That will fail because Sentence.__init__ needs a bunch of variables. But string, words, length, num are calculated by the class so you shouldn't pass them into the initializer. So take them out. Then you get to the next problem.... This is called "test driven development". Write tests that break, fix the problem, write more tests. Commented Nov 6, 2016 at 19:20

3 Answers 3

2

Think of programming as a whack-a-mole game. You have to keep pounding away at the bugs. You can write a series of tests even before you write the program that express how the class should behave. There are various frameworks for doing this including unittest and nose, but the simplest thing is a series of assert statements. All they do is raise an exception if an expression is not true.

So, lets buitd some tests

class Sentence:
    def __init__(self, text, string, words, length, num):
        self.text = sentence
        self.string = str(self.text)
        self.words = self.text.split()
        self.length = len(self.text)
        self.num = len(self.words)

    def getSentence(self):
        return self.string

    def getWords(self):
        return self.words

    def getLength(self):
        return self.length

    def getNumWords(self):
        return self.num

line = "This Tuesday is Election Day"
assert Sentence(line), "can initialize"
assert Sentence(line).getSentence() == line, "can return sentence"
assert Sentence(line).getWords() == ['This', 'Tuesday', 'is',
    'Election', 'Day'], "can return words"
# etc...

I run it and I get

Traceback (most recent call last):
  File "v.py", line 22, in <module>
    assert Sentence(line), "can initialize"
TypeError: __init__() missing 4 required positional arguments: 'string', 'words', 'length', and 'num'

Oops, those parameters are completely unneeded, so I remove them and run again and I get

Traceback (most recent call last):
  File "v.py", line 22, in <module>
    assert Sentence(line), "can initialize"
  File "v.py", line 3, in __init__
    self.text = sentence
NameError: name 'sentence' is not defined

Oops, the parameter name was not correct... Keep doing that until it all works.

Sign up to request clarification or add additional context in comments.

Comments

1

Ok, there's a few things going on here you seem to have misunderstood.

Let's start off easy:

When you do line.getWords(), you try to call the getWords Method of the object line. You seem to think that line is an object of type Sentence but that is not the case. Instead, line is simply an object of type string, because that is what you assigned to it in the first line of your script.

Instead, you need to make an instance of object type Sentence like this:

sentence = Sentence(line)

Then you can invoke the methods you defined in the class on that instance:

sentence.getWords()

As mentioned in one of the comments, there's also problems with the interface of your class. Try to fix those yourself or report back with the exact issue if you get stuck. Solving these kinds of problems yourself is part of the skillset you want to develop.

Comments

0

class Sentence:

def __init__(self, string):

    self.output = string
    self.getSentence = string
    self.getWords = string.split()
    self.getLength = len(list(string))
    self.getNumWords = len(string.split())

def __str__(self):
    return str(self.output)

def getSentence(self):
        self.output = self.getsentence
def getWords(self):
        self.output = self.getWords
def getLength(self):
        self.output = self.getLength
def getNumWords(self):
        self.output = self.getNumWords

You need a def int in your code. The getSentence, getWords, getLength, getNumWords, should only be updating(So you can return in the int). You do not need nor want the return in those def(s).

This is one way of doing it. According to your code ;)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.