我的类的显示数据函数始终不返回
我正在为我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在打印您的函数的值。除非您指定带有 return 的内容,例如
return True
或return 72
,否则打印时函数的值将始终为None
。答案是不打印 John.displayData(),而只是运行它。You are printing the value of your function. Unless you specify something with return, such as
return True
orreturn 72
, the value of the function will always beNone
when printed. The answer is to not printJohn.displayData()
, just run it.您正在打印
John.displayData()
的返回值
。此外,在displayData
函数中,您不会返回任何内容。检查您的函数calculateCommission
和CalculateNetSalary
。在这两个函数中,您将返回一些值,如果您在 print 中调用此函数,那么您在 return 语句中提到的值将被打印。在这种情况下,删除打印并只需调用
displayData
函数,如下所示:You are printing the
return value
ofJohn.displayData()
. Also in thedisplayData
function you are not returning anything. Check your functioncalculateCommission
andCalculateNetSalary
. 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: