我的类的显示数据函数始终不返回

发布于 2025-01-15 06:34:33 字数 2219 浏览 1 评论 0原文

我正在为我的 OOP 课程做一项作业,其中我们创建一个人,然后创建该课程的子级,例如员工。在我的以下代码中,当我尝试使用 displayData 函数打印类中的某些属性或变量时,我总是收到“无”消息,它在打印对象的所有属性或变量后出现,有人知道为什么吗?

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())

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())

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

被你宠の有点坏 2025-01-22 06:34:33

您正在打印您的函数的值。除非您指定带有 return 的内容,例如 return Truereturn 72,否则打印时函数的值将始终为 None。答案是不打印 John.displayData(),而只是运行它。

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.

橘亓 2025-01-22 06:34:33

您正在打印 John.displayData()返回值。此外,在 displayData 函数中,您不会返回任何内容。检查您的函数 calculateCommissionCalculateNetSalary。在这两个函数中,您将返回一些值,如果您在 print 中调用此函数,那么您在 return 语句中提到的值将被打印。

在这种情况下,删除打印并只需调用 displayData 函数,如下所示:

John.displayData()

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文