class Account:
def __init__(self, id = 0, balance = 100, annual_interest_rate = 0):
self.id = int(id)
self.balance = float(balance)
self.annual_interest_rate = float(annual_interest_rate)
def get_id(self):
return self.id
def set_id(self, id):
self.id = id
def get_balance(self):
return self.balance
def set_balance(self, balance):
self.balance = balance
def get_annual_interest_rate(self):
return self.annual_interest_rate
def set_annual_interest_rate(self, annual_interest_rate):
self.annual_interest_rate = annual_interest_rate
def get_monthly_interest_rate(self):
return self.annual_interest_rate/12
def get_monthly_interest(self):
return self.balance*self.get_monthly_interest_rate()
def withdraw(self, withdraw):
self.balance -= withdraw
def deposit(self, deposit):
self.balance = deposit
def main():
account1 = Account(id =1122, balance= 20000, annual_interest_rate= 4.5)
account1.withdraw(2500)
account1.deposit(3000)
print(account1.get_id() )
print(account1.get_balance() )
print(account1.get_monthly_interest_rate() )
print(account1.get_monthly_interest() )
main()
我不明白/無法找出代碼不起作用的原因。
當它是類的名稱時,我收到一條錯誤訊息,指出名稱“帳戶”未定義。
uj5u.com熱心網友回復:
你的身份不好
class Account:
def __init__(self, id = 0, balance = 100, annual_interest_rate = 0):
self.id = int(id)
self.balance = float(balance)
self.annual_interest_rate = float(annual_interest_rate)
# the rest of the class
...
# Bad identation <- move left
def main():
account1 = Account(id =1122, balance= 20000, annual_interest_rate= 4.5)
account1.withdraw(2500)
account1.deposit(3000)
print(account1.get_id() )
print(account1.get_balance() )
print(account1.get_monthly_interest_rate() )
print(account1.get_monthly_interest() )
# Good practice to had this condition especially
# if your module could be imported by another module
if __name__ == '__main__':
main()
uj5u.com熱心網友回復:
在python中,一切都與縮進有關。只需將 main 放在后面,就像這樣:
class Account:
def __init__(self, id=0, balance=100, annual_interest_rate=0):
self.id = int(id)
self.balance = float(balance)
self.annual_interest_rate = float(annual_interest_rate)
def get_id(self):
return self.id
def set_id(self, id):
self.id = id
def get_balance(self):
return self.balance
def set_balance(self, balance):
self.balance = balance
def get_annual_interest_rate(self):
return self.annual_interest_rate
def set_annual_interest_rate(self, annual_interest_rate):
self.annual_interest_rate = annual_interest_rate
def get_monthly_interest_rate(self):
return self.annual_interest_rate / 12
def get_monthly_interest(self):
return self.balance * self.get_monthly_interest_rate()
def withdraw(self, withdraw):
self.balance -= withdraw
def deposit(self, deposit):
self.balance = deposit
def main():
account1 = Account(id=1122, balance=20000, annual_interest_rate=4.5)
account1.withdraw(2500)
account1.deposit(3000)
print(account1.get_id())
print(account1.get_balance())
print(account1.get_monthly_interest_rate())
print(account1.get_monthly_interest())
main()
uj5u.com熱心網友回復:
把你的主要功能放在課堂之外然后呼叫它。您在類本身內部進行了類參考。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/364940.html
上一篇:簡單的邏輯條件標志MATLAB
下一篇:如何訪問子類中的變數?
