0

I'm practicing classes by making an employee management program. I made a class for special needs employees, which has a couple of methods. The method in question is def show_needs().

employer_need = Special_emp("louis", 35, 6000, "Programmer", [])
print(employer_need.show_needs())

The output of this code is "None", although i assigned a list to it. and the output after adding some needs:

employer_need.add_need("Autistic")
employer_need.add_need("Partially blind")
print(employer_need.show_needs())

is this

--> Autistic       
--> Partially blind
None

It prints the list but prints "None" with it too, and I can't understand why.

class Employee:
    Employees = 0
    raise_amount = 1.01
    
    def __init__(self, name, age, pay):
        self.name = name
        self.age = age
        self.pay = pay
        Employee.Employees += 1
        
class Special_emp(Employee):
    def __init__(self, name, age, pay, occupation, special_needs=None):
        super().__init__(name, age, pay)
        self.occupation = occupation
        if special_needs is None:
            self.special_needs = []
        else:
            self.special_needs = special_needs

            
    
    def add_need(self, need):
        if need not in self.special_needs:
            self.special_needs.append(need)
    
    def remove_need(self, need):
        if need in self.special_needs:
            self.special_needs.remove(need)
    
    def show_needs(self):
        for need in self.special_needs:
            print("-->", need)
Moaz_pr
  • 31
  • 7
  • `show_needs` prints output on its own; it doesn't return a string that needs to be printed. – chepner May 23 '23 at 20:00
  • Your function returns nothing, so you print `None`. Call the function on itself, not using print. – B Remmelzwaal May 23 '23 at 20:00
  • Basically, it's the same answer to why `print(print("foo"))` would output `foo` and `None`. – chepner May 23 '23 at 20:01
  • @chepner. Thanks for the quick reply. I understand the problem now, this is my first time knowing that printing a function returns "None" tho. Thanks again, it works perfectly now. – Moaz_pr May 23 '23 at 21:16

0 Answers0