0

I am having a problem calling my class and function to my main code. I created a class and do the method in it and just call it in my main code.

This is my class:

from nltk.corpus import wordnet
class syn:
    def synon():
        f = open('filename', "r")
        first = f.readline()
        print("1. ", first)
        input_ans = input('Answer: ')
        synonyms = []
        for syn in wordnet.synsets(first):
            for l in syn.lemmas():
                synonyms.append(l.name())
        if input_ans == first:
            score += 0
            print("Wrong")
        elif input_ans in set(synonyms):
            print("Correct")
            score += 1
        else:
            score += 0
            print("Wrong")

This is my main code:

import os
from synonyms import syn
from nltk.corpus import wordnet

a = syn()
a.synon()

It does not print anything.

Matthias
  • 12,873
  • 6
  • 42
  • 48
Jay
  • 39
  • 5

1 Answers1

1

Basically, you are missing the self argument. See here or here for more logic behind why it's needed. If you're going to wrap the function in a class, you might find __init__ and other "dunder" methods interesting.

Here is a toy example that will do what you want:

class syn:
    def member_function(self):
        print("hello from syn")
        
a = syn()
a.member_function()
Jacob Lee
  • 4,405
  • 2
  • 16
  • 37
Hasz
  • 13
  • 3
  • There are no class attributes in this case. Method should have `@staticmethod` decorator. – Peter Trcka Nov 25 '21 at 06:50
  • It worked now. I just have a typo. What does it mean when I have no attribute in function? I am having an error from part of score. – Jay Nov 25 '21 at 06:53