Рекурсия в python классах

Есть код:

class Bank:

    def __init__(self, N, R):
        Month = 12 * R

        def deposit(n, month):
            if month == 0:
                return n
            return deposit(n, month - 1) * (1 + 10 / 100 / 12)

        profit = deposit(N, Month)
        print(f'Сумма на счету к концу срока: {profit}')


Bank(14, 10)

Рекурсия работает нормально При попытке улучшить программу с добавлением нового класс все ломается:

class Investment:

    def __init__(self, N, R):
        self.N = N
        self.R = R
        self.Month = 12 * self.R


class Bank:

    def deposit(self, investment):
        if investment.Month == 0:
            return investment.N
        return investment.N + (investment.Month - 1) * (1 + 10 / 100 / 12)


vklad = Investment(14, 10)
bank = Bank()
print(bank.deposit(vklad))

Скажите, как правильно перенести рекурсию?


Ответы (1 шт):

Автор решения: Beger

Решение:

class Investment:

    def __init__(self, N, R):
        self.N = N
        self.R = R
        self.Month = 12 * self.R


class Bank:

    def deposit(self, n, month):
        if month == 0:
            return n
        return self.deposit(n, month - 1) * (1 + 10 / 100 / 12)


bank = Bank()
vklad = Investment(14, 10)
print(bank.deposit(vklad.N, vklad.Month))
→ Ссылка