Home > Enterprise >  Name 'Account' is not defined when it is the name of the class
Name 'Account' is not defined when it is the name of the class

Time:11-24

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()

I do not understand/can't find out why the code won't work.

I get an error saying name 'Account' is not defined, when it is the name of the class.

CodePudding user response:

You have a bad identation

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()

CodePudding user response:

In python everything is about indentations. Just put main to back, like this:

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()

CodePudding user response:

Put you'r main function outside of class then call it. You made class reference while being inside of the class itself.

  • Related