Home > Software engineering >  display data function for my classes always return none
display data function for my classes always return none

Time:03-19

I am doing an assignment for my OOP class in which we create a person, then create children of that class such as an employee. In my following code I always get a "None" message when I try to print some attributes or variables in my classes using my displayData function, it appears after all the attributes or variables of my object are printed, would anyone know why?.

from dataclasses import dataclass

@dataclass

class Person:
    FirstName : str
    LastName : str
    Age : int
    Address : str
    ContactNumber : int

@dataclass

class Employee(Person):
    EmployeeID : str
    OrganizationName : str
    Position : str

@dataclass

class CommissionEmployee(Employee):
    commissionRate : float

    def calculateCommission(self):
        gross_sale = input("Please enter the gross sale amount: ")
        totalEarning = float(gross_sale) * (1   self.commissionRate)
        return totalEarning

    def displayData(self):
        print("First Name:", self.FirstName)
        print("Last Name:", self.LastName)
        print("Age:", self.Age)
        print("Address:", self.Address)
        print("Contact Number:", self.ContactNumber)
        print("Employee ID:", self.EmployeeID)
        print("Organization Name:", self.OrganizationName)
        print("Position:", self.Position)
        print("Commission Rate:", self.commissionRate)
        print("Total Earnings:", "${:,.2f}".format(self.calculateCommission()))

@dataclass

class SalariedEmployee(Employee):
    baseSalary : float

    def CalculateNetSalary(self):
        provisionalTax = 0.13 * self.baseSalary
        insurance = 0.01 * self.baseSalary
        fedTax = 0.03 * self.baseSalary
        NetSalary = self.baseSalary - provisionalTax - insurance - fedTax
        return "${:,.2f}".format(NetSalary)

    def displayData(self):
        print("First Name:", self.FirstName)
        print("Last Name:", self.LastName)
        print("Age:", self.Age)
        print("Address:", self.Address)
        print("Contact Number:", self.ContactNumber)
        print("Employee ID:", self.EmployeeID)
        print("Organization Name:", self.OrganizationName)
        print("Position:", self.Position)
        print("Base Salary:", "${:,.2f}".format(self.baseSalary))
        print("Net Salary:", self.CalculateNetSalary())

John = SalariedEmployee("John", "Smith", 21, "21 Cool Beans Dr", 123456789, "201", "Tesla", "CEO", 100.0)

print (John.displayData())

CodePudding user response:

You are printing the value of your function. Unless you specify something with return, such as "return True" or "return 72", the value of the function will always be None when printed. The answer is to not print John.displayData(), just run it.

CodePudding user response:

You are printing the return value of John.displayData(). Also in the displayData function you are not returning anything. Check your function calculateCommission and CalculateNetSalary. In these two functions you are returning some value and if you call this function inside print then the value you mentioned in return statement will get printed.

In this case remove the print and just call displayData function as follows:

John.displayData()
  • Related