1

I'm creating a class with function within, but i keep getting error "name 'direct_report' is not defined"

Basically I'm trying to make an organization chart, creating a list using the direct_report function to add people under each position

class employee:
def __init__(self, name , title, salary):
    self.name = name
    self.title = title
    self.salary = salary
    self.direct_reports_list = direct_report()

def __str__(self):
    #otheremp_list = []
    print(self.title,'-', self.name)
    print('Direct Reports:')
    for emp in self.direct_reports_list:
        print(emp.title,'-', emp.name)
    #    otheremp_list.append(emp.direct_reports_list)
    #print('other employees:')
    #for emp in otheremp_list:
    #    print(emp.title,'-', emp.name)
    #    otheremp_list.append(emp.direct_reports_list)

def direct_report(self,value):
    print(value)
    direct_reports_list = []
    direct_reports_list.append(value)
    print(direct_reports_list)
    return direct_reports_list

ceo = employee("Elon Musk", "CEO",1000000)
devdir = employee("Jeff Bezos","Development Director",500000)
devassoc1 = employee("Beyonce Knowles","Development Associate", 50000)
devassoc2 = employee("Taylor Swift","Development Associate", 50000)
ceo.direct_report(devdir)
ceo.direct_report(devdir2)
devdir.direct_report(devassoc1)
devdir.direct_report(devassoc2)
print(ceo)

The # is my further plan to print the full organization chart, but currently I'm still stuck at the "direct report" parts.

4 Answers 4

1

You need to add one indentation level for the classes methods like that:

class employee:
    def __init__(self, name , title, salary):
        self.name = name
        self.title = title
        self.salary = salary
        self.direct_reports_list = direct_report()

    def __str__(self):
        #otheremp_list = []
        print(self.title,'-', self.name)
        print('Direct Reports:')
        for emp in self.direct_reports_list:
            print(emp.title,'-', emp.name)
        #    otheremp_list.append(emp.direct_reports_list)
        #print('other employees:')
        #for emp in otheremp_list:
        #    print(emp.title,'-', emp.name)
        #    otheremp_list.append(emp.direct_reports_list)

    def direct_report(self,value):
        print(value)
        direct_reports_list = []
        direct_reports_list.append(value)
        print(direct_reports_list)
        return direct_reports_list

ceo = employee("Elon Musk", "CEO",1000000)
devdir = employee("Jeff Bezos","Development Director",500000)
devassoc1 = employee("Beyonce Knowles","Development Associate", 50000)
devassoc2 = employee("Taylor Swift","Development Associate", 50000)
ceo.direct_report(devdir)
ceo.direct_report(devdir2)
devdir.direct_report(devassoc1)
devdir.direct_report(devassoc2)
print(ceo)
Sign up to request clarification or add additional context in comments.

2 Comments

Could you explain more what do you mean by adding indentation level? thank you for replying though!
@W.tan I mean the tabs you put at the beginning of your function
1

Call the function in this way.

class employee:
    def __init__(self, name , title, salary):
        self.name = name
        self.title = title
        self.salary = salary
        self.direct_reports_list = self.direct_report()

1 Comment

I got this error now "direct_report() missing 1 required positional argument: 'value'"
0

Try this code.

class employee:
    def __init__(self, name , title, salary):
        self.name = name
        self.title = title
        self.salary = salary
        self.direct_reports_list = []

    def __str__(self):
        #otheremp_list = []
        print(self.title,'-', self.name)
        print('Direct Reports:')
        for emp in self.direct_reports_list:
            print(emp.title,'-', emp.name)
        #    otheremp_list.append(emp.direct_reports_list)
        #print('other employees:')
        #for emp in otheremp_list:
        #    print(emp.title,'-', emp.name)
        #    otheremp_list.append(emp.direct_reports_list)
        return "Done"

    def direct_report(self,value):
        self.direct_reports_list.append(value)

ceo = employee("Elon Musk", "CEO",1000000)
devdir = employee("Jeff Bezos","Development Director",500000)
devassoc1 = employee("Beyonce Knowles","Development Associate", 50000)
devassoc2 = employee("Taylor Swift","Development Associate", 50000)
ceo.direct_report(devdir)
devdir.direct_report(devassoc1)
devdir.direct_report(devassoc2)
print(ceo)
print(devdir)
print(devassoc1)
print(devassoc2)

1 Comment

This one works well! thank you!!
0

try self.direct_report() instead as you are calling a method within the class

1 Comment

I got this error now "direct_report() missing 1 required positional argument: 'value"

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.