0

In Python, I am trying to assigne a value to a variable in one method and then add a another value to the same variable in another method. Both methods are within the same class. Here is what I am trying to achieve:

class Account_Balance(Account_General):
    def __init__(self, first_name, middle_name, last_name, PIN, DoB, city, address, phone_number, email):
        super().__init__(first_name, middle_name, last_name, PIN, DoB, city, address, phone_number, email)
    
    def account_balance(self):
        self.balance = 10.0 
        return self.balance
    
    def account_deposite(self, amount):
        self.amount = amount
        self.balance += self.amount

The output is: Attribute Error: 'Account_Balance' object has no attribute 'balance'

Where do you think I am getting it wrong? Thank you!

1 Answer 1

1

You're calling account_deposite before self.balance has been initialized. Attributes like this should be declared and initialized in __init__. Try this

class Account_Balance(Account_General):
    def __init__(self, first_name, middle_name, last_name, PIN, DoB, city, address, phone_number, email):
        super().__init__(first_name, middle_name, last_name, PIN, DoB, city, address, phone_number, email)
        self.balance = 0.0
    
    def account_balance(self):
        self.balance = 10.0 
        return self.balance
    
    def account_deposite(self, amount):
        self.amount = amount
        self.balance += self.amount
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.