Help with subclasses and classes
Terry Reedy
tjreedy at udel.edu
Sun Jul 3 02:30:00 EDT 2016
On 7/2/2016 8:58 PM, cadenmrice at gmail.com wrote:
> When i run the code I get this error :
> Traceback (most recent call last):
> File "C:/Python25/chapter2", line 18, in <module>
> bank=InterestAccount(5,1000,0.5)
> NameError: name 'InterestAccount' is not defined
>
> I can't find the issue, any help?
>
>
> class BankAccount:
> def __init__(self,numb,bal):
> self.numb = numb
> self.bal = bal
> def showbal(self):
> print "Your balance is", self.bal
> def withdraw(self,wa):
> self.bal-=wa
> print("Your balance is now", self.bal)
> def deposit(self,da):
> bal+=da
> print("Your balance is now", self.bal)
> class InterestAccount():
> def BankAccount__init__(self,numb,bal,intrate):
> self.intrate=intrate
> def addintrest(self):
> self.bal = self.bal * self.intrate
InterestAccount should not be nested. It should subclass BankAccount.
You should write the subclass init as shown. Do put spaces after commas.
class InterestAccount(BankAccount):
def __init__(self, numb, bal, intrate):
BankAccount(self, numb, bal)
self.intrate=intrate
def addintrest(self):
self.bal = self.bal * self.intrate
> bank=InterestAccount(5, 1000, 0.5)
> bank.addintrest()
> bank.showbal()
I believe the above will not do as you expect.
--
Terry Jan Reedy
More information about the Python-list
mailing list