Python Practice › OOP Programs › Program 31
SavingsAccount with interest
Create SavingsAccount (inherits BankAccount) with interest_rate and minimum_balance, and a method to calculate interest.
Output
800.0 0
Approach
- Interest is earned only when the balance is at least the minimum.
Solution
class BankAccount: def __init__(self, account_number, balance=0): self.account_number, self.balance = account_number, balance class SavingsAccount(BankAccount): def __init__(self, account_number, balance, interest_rate, minimum_balance): super().__init__(account_number, balance) self.interest_rate = interest_rate self.minimum_balance = minimum_balance def interest(self): if self.balance < self.minimum_balance: return 0 return round(self.balance * self.interest_rate / 100, 2) print(SavingsAccount("S-1", 20000, 4, 5000).interest())print(SavingsAccount("S-2", 3000, 4, 5000).interest())Output
800.0 0
Learn the concept
New to this topic? Read the lesson, then come back.