Last active
June 25, 2020 22:52
-
-
Save victorusachev/ac1fa85e80e4c3ca928226c87703323f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from collections import namedtuple | |
| CreditCondition = namedtuple( | |
| 'CreditCondition', | |
| ['property_value', | |
| 'initial_payment', | |
| 'loan_months', | |
| 'interest_rate']) | |
| class CreditCalculator: | |
| def __init__(self, | |
| property_value, | |
| initial_payment, | |
| loan_months, | |
| interest_rate): | |
| self.property_value = property_value | |
| self.initial_payment = initial_payment | |
| self.loan_months = loan_months | |
| self.interest_rate = interest_rate | |
| @property | |
| def loan_amount(self): | |
| return self.property_value - self.initial_payment | |
| @property | |
| def monthly_payment(self): | |
| coef_months = (1 + self.interest_rate / 12) ** self.loan_months | |
| return self.loan_amount * self.interest_rate / 12 * coef_months / (coef_months - 1) | |
| @property | |
| def overpayment(self): | |
| return self.monthly_payment * self.loan_months - self.loan_amount | |
| def main(): | |
| while True: | |
| print('Введите данные для расчёта кредита') | |
| fields = dict( | |
| property_value=dict(text='Стоимость недвижимости', value=0), | |
| initial_payment=dict(text='Первоначальный взнос', value=0), | |
| loan_months=dict(text='Период кредита в месяцах', value=0), | |
| interest_rate=dict(text='Процентная ставка (%)', value=0), | |
| ) | |
| for code, field in fields.items(): | |
| value = input(f"{field['text']}: ") | |
| try: | |
| value = float(value) | |
| if value > 0: | |
| fields[code]['value'] = value | |
| except Exception as ex: | |
| print(f'Введено некорректное значение: {value}') | |
| print(ex) | |
| break | |
| data = CreditCondition(*[field['value'] | |
| for field in fields.values()]) | |
| credit = CreditCalculator( | |
| property_value=data.property_value, | |
| initial_payment=data.initial_payment, | |
| loan_months=data.loan_months, | |
| interest_rate=data.interest_rate / 100) | |
| print('Стоимость недвижимости:', credit.property_value) | |
| print('Первоначальный взнос:', credit.initial_payment) | |
| print('Процентная ставка (%)', credit.interest_rate * 100) | |
| print('Период кредита в месяцах:', credit.loan_months) | |
| print('Размер кредита:', credit.loan_amount) | |
| print('Ежемесячный платёж:', credit.monthly_payment) | |
| print('Переплата:', credit.overpayment) | |
| if input('Для выхода введите exit: ').lower() == 'exit': | |
| break | |
| print('\n') | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment