我的代碼在python下面一行:AttributeError的在我的Python代碼2
class BankAccount:
def __init__ (self,balance = 0):
self.balance = balance
def deposit(self, deposit_amount= 30):
self.deposit_amount=deposit_amount
balance += deposit_amount
return balance
def withdraw (self,withdraw_amount= 10):
if withdraw_amount > balance:
raise RuntimeError('Invalid Transaction')
balance -= withdraw_amount
return balance
class MinimumBalanceAccount(BankAccount):
def __init__(self,balance = 0):
self.balance = balance
c = BankAccount()
c.deposit(50)
它給了我這個錯誤:
AttributeError("BankAccount instance has no attribute 'deposit'"
更換
c = BankAccount()
做你已經有了一些縮進和格式問題 – CoryKramer你的縮進遍佈各處。你能糾正它,請匹配你的實際代碼? –
不相關但很重要:在Python 2中,_always_從'object'派生頂級類:即,像這樣定義它:'class BankAccount(object):...'。如果你不這樣做,你將看不到所有記錄的對象和類的行爲。 – alexis