2017-08-11 39 views
0
import datetime 


class Loan: 
    def __init__(self, principal, rate, date): 
     self.p=principal 
     self.r=rate 
     self.d=date 

    def amount(self): 
     interest=self.r*self.p 
     now=datetime.date.today() 
     delta=datetime.timedelta(1) 
     then=self.d 
     while then <= now: 
      print then.strftime("%Y-%m-%d") 
      print outstanding 
      outstanding +=interest 
      then+=delta 


x=Loan 
x.p=10000 
x.r=0.1/7 
x.d=datetime.date.today()- 
    datetime.timedelta(5) 
x.amount() 

我希望得到每個區間的日期和餘額,但我得到一個錯誤,我查了一下過去的解決方案和用我的代碼進行交叉檢查以確保該方法在實例上被調用。當我運行的代碼中,我得到的錯誤是:類型錯誤:不受約束的方法量()必須與貸款實例被稱爲第一個參數(什麼都沒有代替)

類型錯誤:不受約束的方法量()必須與貸款實例被稱爲第一個參數(什麼都沒有代替)

+0

在操作其方法之前,您首先要將Loan類實例化爲一個對象(使用圓括號Loan()),請嘗試使用p = 10000 r = 0.1/7 d = datetime.date .today() - datetime.timedelta(5) x =貸款(p,r,d)' – davedwards

+1

x = Loan()完全解決。留言Merci。 x = Loan(p,r,d)給出錯誤'構造函數不帶參數'。因此,一次實例化每個參數可以解決它。 – Paul

回答

0
import datetime 


class Loan: 
    def __init__(self, principal = 0, rate = 0, date = 0): 
     self.p=principal 
     self.r=rate 
     self.d=date 

    def amount(self): 
     interest=self.r*self.p 
     now=datetime.date.today() 
     delta=datetime.timedelta(1) 
     then=self.d 
     outstanding = 0 
     while then <= now: 
      print then.strftime("%Y-%m-%d") 
      print outstanding 
      outstanding +=interest 
      then+=delta 


x= Loan() 
x.p=10000 
x.r=0.1/7 
x.d=datetime.date.today()-datetime.timedelta(5) 
x.amount() 

給予默認值來構造ARGS 。

相關問題