如何将员工添加到经理中';谁的主管?(Python类)

如何将员工添加到经理中';谁的主管?(Python类),python,class,Python,Class,我有一个Employee类和一个Manager类,创建的每个Employee都将获得Employee类甚至Manager的属性 我的问题是,我想创建一个输入选项,要求经理输入他希望添加到其监督中的员工(将其添加到其员工列表中),并同时添加员工属性 (我知道最后3条线路有故障,我就是想不出来) 如果你不熟悉字典,我会给你一个简短的概述,但你应该认真阅读,以及维基百科的一般条目 现在回答你的实际问题。我构建了一个员工字典,并从字典中将员工的密钥添加到经理。我还展示了构建字典的两种方法:一种是在创建d

我有一个Employee类和一个Manager类,创建的每个Employee都将获得Employee类甚至Manager的属性

我的问题是,我想创建一个输入选项,要求经理输入他希望添加到其监督中的员工(将其添加到其员工列表中),并同时添加员工属性 (我知道最后3条线路有故障,我就是想不出来)


如果你不熟悉字典,我会给你一个简短的概述,但你应该认真阅读,以及维基百科的一般条目

现在回答你的实际问题。我构建了一个员工字典,并从字典中将员工的密钥添加到经理。我还展示了构建字典的两种方法:一种是在创建
dict
时添加值,另一种是稍后添加值

class Employee:

        def __init__(self,first,last,pay):
                self.first = first
                self.last = last
                self.pay = pay
                self.email = first+'.'+last+'@company.com'

        def fullname(self):
                return '{} {}'.format(self.first,self.last)

class Manager(Employee):

        def __init__(self,first,last,pay,employees=None):
                super().__init__(first,last,pay)
                if employees is None:
                    self.employees = []
                else:
                    self.employees = employees

        def add_emps(self,emp):
                if emp not in self.employees:
                    self.employees.append(emp)
                else:
                    print('the employee is already in your supervise')

        def print_emps(self):
                for em in self.employees:
                    print('-->',em.fullname())


employee_dict = {
    'Mohamad_Ibrahim': Employee('Mohamad','Ibrahim',90000),
    'Bilal_Tanbouzeh': Employee('Bilal','Tanbouzeh',110000)
}
employee_dict['Ghalia_Awick'] = Employee('Ghalia','Awick',190000)
employee_dict['Khaled_Sayadi'] = Employee('Khaled','Sayadi',80000)

mngr_1 = Manager('Ibrahim','othman',200000,[employee_dict['Mohamad_Ibrahim'],employee_dict['Bilal_Tanbouzeh']])
mngr_2 = Manager('Rayan','Mina',200000,[employee_dict['Ghalia_Awick'],employee_dict['Khaled_Sayadi']])

add_them = input('enter the employee you would like to add') # Expects the name like the keys are Firstname_Lastname

mngr_1.add_emps(employee_dict[add_them])

mngr_1.print_emps()

您有四名员工,但还没有经理。mngr_1是管理emp_1和emp_2的经理,但我希望有一个输入选项,可以询问他希望将哪个emp添加到其管理列表中。您必须从输入中获取员工实例。将所有员工放入字典中以便轻松检索可能是个好主意。您知道如何从输入中获取员工实例吗?对于另一个解决方案,即使我把它们都放在字典里,我该如何检查它们并把它们取出来呢?没问题,如果答案满足你的问题,一定要把它标记为“已回答”
a = {} # create an empty dictionary
a['some_key'] = "Some value" # Equivalent of creating it as a = {'some_key': "Some value"}
# Dictionaries are stored in "key, value pairs" that means one key has one value.
# To access the value for a key, we just have to call it
print(a['some_key'])
# What if we want to print all values and keys?
for key in a.keys():
    print("Key: " + key + ", Value: " + str(a[key]))
class Employee:

        def __init__(self,first,last,pay):
                self.first = first
                self.last = last
                self.pay = pay
                self.email = first+'.'+last+'@company.com'

        def fullname(self):
                return '{} {}'.format(self.first,self.last)

class Manager(Employee):

        def __init__(self,first,last,pay,employees=None):
                super().__init__(first,last,pay)
                if employees is None:
                    self.employees = []
                else:
                    self.employees = employees

        def add_emps(self,emp):
                if emp not in self.employees:
                    self.employees.append(emp)
                else:
                    print('the employee is already in your supervise')

        def print_emps(self):
                for em in self.employees:
                    print('-->',em.fullname())


employee_dict = {
    'Mohamad_Ibrahim': Employee('Mohamad','Ibrahim',90000),
    'Bilal_Tanbouzeh': Employee('Bilal','Tanbouzeh',110000)
}
employee_dict['Ghalia_Awick'] = Employee('Ghalia','Awick',190000)
employee_dict['Khaled_Sayadi'] = Employee('Khaled','Sayadi',80000)

mngr_1 = Manager('Ibrahim','othman',200000,[employee_dict['Mohamad_Ibrahim'],employee_dict['Bilal_Tanbouzeh']])
mngr_2 = Manager('Rayan','Mina',200000,[employee_dict['Ghalia_Awick'],employee_dict['Khaled_Sayadi']])

add_them = input('enter the employee you would like to add') # Expects the name like the keys are Firstname_Lastname

mngr_1.add_emps(employee_dict[add_them])

mngr_1.print_emps()