Search code examples
pythonclasssubclass

Python Classes exercise not working from parent class


I have the following exercise, but I am unable to understand completely how to make it work.

Develop a program that consists of:

1- A parent class Account that contains:

Attributes:

  • text type Name
  • integer type quantity(money in the account)

Also cointains a Method that returns the data in the Account class.

2- Define two classes FixedTime and Savings that inherit from the Account class, where

The Savings class

  • Define the init that passes the data inherited from the Account Class
  • You will have a method that prints the information.

The FixedTime class will have:

Define the init that passes the data inherited from the Account Class as well to have:

  • attribute term type integer
  • floating rate interest attribute.

Methods:

  • Method to obtain the amount of interest to be returned by the result of (amount * interest / 100) 2. Method to display the information, data of the holder, term, interest and total of interest. Create a main program where you define a variable of the class FixedTime and Savings, Fill in the necessary information and validate the functionality of each indicated method.

I have the following code:

class Account:
    def __init__(self, name, amount):
        self.name = name
        self.amount = amount

    def get_name(self):
        return self.name
    def get_amount(self):
        return self.amount

class Saving(Account):
    def __init__(self, name, amount):
        Account.__init__(self, name, amount)
    def  get_info(self):
        print(f"{self.name}, {self.amount}")

class Fixrate(Account):
    def __init__(self, rate, time):
        self.rate = rate
        self.time = time
    def total_import(self):
        total = (self.amount * self.rate)/ 100
        return total
    def rate_info_print(self):
        print(f"Name: {self.name} term: {self.time} rate: {self.rate} Total rate: {self.total_import()}")


person = Account("andres", 5000)
date = Fixrate(4.5, 4)
Saving.get_info(person)
Fixrate.rate_info_print(person)

I am getting this error:

print(f"Name: {self.name} term: {self.time} rate: {self.rate} Total rate: {self.total_import()}") AttributeError: 'Account' object has no attribute 'time'


Solution

    • you need assign time and name in Fixrate __init__
    • you create a instance to Fixrate called date, then you should use it rather than use Fixrate.rate_info_print(person)

    code:

    class Account:
        def __init__(self, name, amount):
            self.name = name
            self.amount = amount
    
        def get_name(self):
            return self.name
        def get_amount(self):
            return self.amount
    
    class Saving(Account):
        def __init__(self, name, amount):
            super().__init__(name, amount)
        def get_info(self):
            print(f"{self.name}, {self.amount}")
    
    class Fixrate(Account):
        def __init__(self, rate, time, name, amount):
            self.rate = rate
            self.time = time
            super(Fixrate, self).__init__(name,amount)
    
        def total_import(self):
            total = (self.amount * self.rate)/ 100
            return total
            
        def rate_info_print(self):
            print(f"Name: {self.name} term: {self.time} rate: {self.rate} Total rate: {self.total_import()}")
    
    
    person = Account("andres", 5000)
    date = Fixrate(4.5, 4,"andres",5000)
    Saving.get_info(person)
    date.rate_info_print()
    

    result:

    andres, 5000
    Name: andres term: 4 rate: 4.5 Total rate: 225.0
    

    or if you want to make use of person, you can try this one.

    code:

    class Account:
        def __init__(self, name, amount):
            self.name = name
            self.amount = amount
    
        def get_name(self):
            return self.name
        def get_amount(self):
            return self.amount
    
    class Saving(Account):
        def __init__(self, name, amount):
            super().__init__(name, amount)
        def get_info(self):
            print(f"{self.name}, {self.amount}")
    
    class Fixrate(Account):
        def __init__(self, rate, time, person):
            self.rate = rate
            self.time = time
            self.person = person
            # super(Fixrate, self).__init__(name,amount)
    
        def total_import(self):
            total = (self.amount * self.rate)/ 100
            return total
            
        def rate_info_print(self):
            self.name = person.get_name()
            self.amount = person.get_amount()
            print(f"Name: {self.name} term: {self.time} rate: {self.rate} Total rate: {self.total_import()}")
    
    
    person = Account("andres", 5000)
    date = Fixrate(4.5, 4,person)
    Saving.get_info(person)
    date.rate_info_print()
    

    result:

    andres, 5000
    Name: andres term: 4 rate: 4.5 Total rate: 225.0