2017-05-07 127 views
3

我應該編寫一個名爲Employee的類,它包含名稱,工資和服務年限的數據。它計算員工在退休時將收到的每月養老金支出,並顯示Employee對象的三個實例變量。它還調用Employee對象的養老金方法來計算此員工的每月養老金支出並顯示它。運行python程序後沒有輸出

運行我的程序後,我沒有得到任何輸出,這是特定的問題。

我應該得到這樣的輸出:

Name: Joe Chen 
Salary: 80000 
Years of service: 30 
Monthly pension payout: 3600.0 

Process finished with exit code 0 

下面是完整的代碼。

class Employee: 


    # init method implementation 
    def __init__(self, EmpName, EmpSalary, EmpYos): 
     self.EmpName = EmpName 
     self.EmpSalary = EmpSalary 
     self.EmpYos = EmpYos 

    def displayEmployeeDetails(self): 
     print "\nName ", self.EmpName, 

    # defines the salary details 
    def displaySalary(self): 
     print "\nSalary", self.EmpSalary 

    # defines the years of service 
    def displayYoservice(self): 
     print "\nYears of Service", self.EmpYos 

    # defines pension 
    def MonthlypensionPayout(self): 
     print "\nMonthly Pension Payout:", self.EmpSalary * self.EmpYos * 0.0015 


def main(): 


    # creates instance for employee 1 
    Emplo1 = Employee("Joe Chen", 80000, 30) 
    # creates instance for employee 2 
    Emplo2 = Employee("Jean park", 60000, 25) 

    # Function calls 
    Emplo1.displayEmployeeDetails() 
    Emplo1.displaySalary() 
    Emplo1.displayYoservice() 
    Emplo1.MonthlypensionPayout() 
    # function calls 
    Emplo2.displayEmployeeDetails() 
    Emplo2.displaySalary() 
    Emplo2.displayYoservice() 
    Emplo2.MonthlypensionPayout() 

main() 
+0

你的Python版本是什麼? – Exprator

+0

刪除def main:和main() – Exprator

+0

@Exprator我認爲Python的版本是2,因爲他沒有括號打印。 –

回答

5

你只是在打印一個空行。將您的功能更改爲以下內容:

def displayEmployeeDetails(self): 

    print \ 
    "\nName ", self.EmpName 

# defines the salary details 
def displaySalary(self): 


print \ 
"\nSalary", self.EmpSalary 

# defines the years of service 
def displayYoservice(self): 


print \ 
"\nYears of Service", self.EmpYos 

python中的\是續行。更好的是把它放在同一條線上,如下所示:

def displayEmployeeDetails(self): 

    print "\nName ", self.EmpName 

# defines the salary details 
def displaySalary(self): 


print "\nSalary", self.EmpSalary 

# defines the years of service 
def displayYoservice(self): 


print "\nYears of Service", self.EmpYos 

查看demo