0

Here is part of my code:

class PhraseTrigger(Trigger):

    def __init__(self, phrase):
        self.phrase = phrase.lower()

    def is_phrase_in(self, text):
        text = text.lower()
        for n in string.punctuation:
            text = text.replace(n, ' ')
        text = text.split()
        text = ' '.join(text)
        if self.phrase in text:
            return True
        else:
            return False

class TitleTrigger(PhraseTrigger):
    def __init__(self, phrase):
        PhraseTrigger.__init__(self, phrase)

    def evaluate(self, story):
        return self.is_phrase_in(story)

So when I want to run my program like this:

a = TitleTrigger('purple cow')
a.evaluate('purple@#$%cow')

I get error messages like this:

Traceback (most recent call last):
File "<ipython-input-41-7901a9ad2481>", line 1, in <module>
    a.evaluate('purple@#$%cow')

File "C:/Users/Dexter/Desktop/mit psets/pset5/pset5/ps5.py", line 121, in evaluate
    return self.is_phrase_in(story)

NameError: name 'is_phrase_in' is not defined

I think I can call the function from base class just with self.function_name. Can someone help?

martineau
  • 119,623
  • 25
  • 170
  • 301
Nin
  • 41
  • 2
  • 3
    Please provide a [mcve]. I get `NameError: name 'Trigger' is not defined` when I try to run your code. – Code-Apprentice Mar 13 '20 at 17:25
  • Why are `is_phrase_in` and `evaluate` methods in a class, rather than standalone functions? – chepner Mar 13 '20 at 17:29
  • Runs without error for me. Can you provide some code that makes the problem reproducible? – a_guest Mar 13 '20 at 17:30
  • I think this might be helpful: https://stackoverflow.com/questions/4747397/calling-base-class-method-in-python – Roy Levy Mar 13 '20 at 17:31
  • @Code-Apprentice ? `string.punctuation` is still defined in Python 3. – chepner Mar 13 '20 at 17:42
  • @chepner Yah, I found that now. Googling "python string" gave the 2.7 version of the docs first. – Code-Apprentice Mar 13 '20 at 17:43
  • 3
    I do kind of wish search engines could start deprioritizing the Python 2 versions of the documentation. I wonder if there's anything the PSF can do to help that along – chepner Mar 13 '20 at 17:44
  • 1
    @Nin I am unable to get the error message you are asking about. I had to make two changes to your code to get output, but neither would affect the error: https://repl.it/@codeguru/VitalHotWeb – Code-Apprentice Mar 13 '20 at 17:45
  • I cannot reproduce the problem either (after adding `import string` and `class Trigger: pass`), so I could run it. – martineau Mar 13 '20 at 18:19
  • Is OP perhaps searching for the super function? – John Mar 13 '20 at 19:09

0 Answers0