Home > Software engineering >  Is there a different way to define a class that isn't the initial creation of the class?
Is there a different way to define a class that isn't the initial creation of the class?

Time:10-26

I am not sure what I have done wrong. I went through the code many times and I cannot figure out why it isn't working. I thought when you create a class then you do not have to 'define' it when creating instances of it?

Here is the code:

class BankAccount:
    #init method with 3 parameters(self, balance & name)
    def __init__(self, balance, name):
       
       #variables and their assignments
       self.balance = balance
       self.name = name
        
       num_deposits = 0
       deposit_total = 0
       num_withdraws = 0
       withdraw_total = 0
     
    #numDeposit method with 1 parameter, itself.
    def numDeposits(self):
        
        #refer to num_deposits instance and increment by 1
        num_deposit  = 1
        
    #'numWithdraw' method
    def numWithdraws(self):
        
        #update instance
        num_withdraws  = 1
        
    #method named 'Deposit" that takes in 2 parameters(self & amount)
    def Deposit(self, amount):
        
        #update balance instance by (amount)
        balance  = amount
        
        #update deposit_total instance by amount
        deposit_total  = amount
        
        #calling numDeposit method to update number of total deposits
        numDeposits()
        
    #method named 'withdraw' that takes in 2 parameters(self & amount)
    def withdraw (self, amount):
        
        #updating instance variables 'balance' and 'withdraw_total'
        balance -= 1
        withdraw_total  = 1
        
        #calling 'numWithdraws' method
        numWithdraws()
        
    # 'endOfMonth' method that has 1 parameter (self)
    def endOfMonth(self):
        
        #print statements
        print("Bank account: ", name)
        print("Balance : ", balance)
        print("Number of deposits: ", num_deposit, "totalling ", deposit_total)
        print("Number of withdraws: ", num_withdraws, "totalling ", withdraw_total)
        
    # 2 instances of BankAccount
    BankAccount1 = BankAccount( 0 , "chase" )
    BankAccount2 = BankAccount( 100, "Bank of America")
    
    #invoking deposit and withdraw for instance 1
    BankAccount1.deposit(50)
    BankAccount1.deposit(50)
    BankAccount1.withdraw(100)
    BankAccount1.withdraw(100)
    BankAccount1.withdraw(100)
    #calling endOfMonth method for instance 1
    BankAccount1.endOfMonth()
    
    #invoking deposit and withdraw for instance 2
    BankAccount2.deposit(25)
    BankAccount2.deposit(25)
    BankAcconut2.deposit(5000)
    BankAccount2.withdraw(10)
    BankAccount2.withdraw(1000)
    BankAccount2.withdraw(70)
    #calling endOfMOnth method for instance 2
    BankAccount2.endOfMonth()

Every time I run the code I get a NameError which says that the name 'BankAccount' is not defined? My desired result is :

Bank Account: Chase Balance: $-200 num of depo : 2 total $100 num of withdraws: 3 total $300

and so on for the second instance of Bank Account

CodePudding user response:

You have a few issues with indentation, and also syntax.

When you work with class attributes, if you want to alter an attribute of that instance of the class, or use a method, you need to use the self. prefix:

def numDeposits(self):
        
    num_deposits  = 1    # this just alters an undefined variable called num_deposits
    self.num_deposits  = 1    # this is what you need

Secondly, your attributes need defining outside of the scope of any methods to work how you want them to.

There are a few other things you could improve like docstrings being descriptive but not redundant, or there's no point in using them (init method with 3 parameters is self-explanitory).


class BankAccount:

    num_deposits = 0
    deposit_total = 0
    num_withdraws = 0
    withdraw_total = 0
    #init method with 3 parameters(self, balance & name)

    def __init__(self, balance, name):
       #variables and their assignments
       self.balance = balance
       self.name = name
        
     
    #numDeposit method with 1 parameter, itself.
    def numDeposits(self):
        
        #refer to num_deposits instance and increment by 1
        self.num_deposits  = 1
        
    #'numWithdraw' method
    def numWithdraws(self):
        
        #update instance
        self.num_withdraws  = 1
        
    #method named 'Deposit" that takes in 2 parameters(self & amount)
    def deposit(self, amount):
        
        #update balance instance by (amount)
        self.balance  = amount
        
        #update deposit_total instance by amount
        self.deposit_total  = amount
        
        #calling numDeposit method to update number of total deposits
        self.numDeposits()
        
    #method named 'withdraw' that takes in 2 parameters(self & amount)
    def withdraw (self, amount):
        
        #updating instance variables 'balance' and 'withdraw_total'
        self.balance -= 1
        self.withdraw_total  = 1
        
        #calling 'numWithdraws' method
        self.numWithdraws()
        
    # 'endOfMonth' method that has 1 parameter (self)
    def endOfMonth(self):
        
        #print statements
        print("Bank account: ", self.name)
        print("Balance : ", self.balance)
        print("Number of deposits: ", self.num_deposits, "totalling ", self.deposit_total)
        print("Number of withdraws: ", self.num_withdraws, "totalling ", self.withdraw_total)
        
# 2 instances of BankAccount
BankAccount1 = BankAccount( 0 , "chase" )
BankAccount2 = BankAccount( 100, "Bank of America")

#invoking deposit and withdraw for instance 1
BankAccount1.deposit(50)
BankAccount1.deposit(50)
BankAccount1.withdraw(100)
BankAccount1.withdraw(100)
BankAccount1.withdraw(100)
#calling endOfMonth method for instance 1
BankAccount1.endOfMonth()

#invoking deposit and withdraw for instance 2
BankAccount2.deposit(25)
BankAccount2.deposit(25)
BankAccount2.deposit(5000)
BankAccount2.withdraw(10)
BankAccount2.withdraw(1000)
BankAccount2.withdraw(70)
#calling endOfMOnth method for instance 2
BankAccount2.endOfMonth()

CodePudding user response:

The bankAccount instances that you create, are still indented within the class. This makes that the bankAccount calls in there are done in the class creation. As the class doesn't exists yet in the creation process, it raises NameError. You should therefore unindent them. If you really want to set them as class atributes, you shoul use bankAccount.BankAccount1=... unindented.

CodePudding user response:

Based on the indentation, it looks like you are creating BankAccount1 and 2 and using them within the definition of class BankAccount? Within that namespace, BankAccount do not exist.

  • Related