Ce script fait partie des projets Python de HyperSkill.
j'ai fait toute la formation python de Hyperskill pendant le Confinement 2020.
# write your code here | |
from math import log, ceil, floor | |
import argparse | |
ap = argparse.ArgumentParser() | |
ap.add_argument("--type", type=str, default=None) | |
ap.add_argument("--principal", type=float, default=None) | |
ap.add_argument("--interest", type=float, default=None) | |
ap.add_argument("--payment", type=float, default=None) | |
ap.add_argument("--periods", type=int, default=None) | |
class Credits: | |
def __init__(self): | |
self.args = vars(ap.parse_args()) | |
self.error = False | |
if (self.args["type"] not in ["annuity", "diff"]) or \ | |
(self.args["type"] == "diff" and self.args["payment"]) or \ | |
(self.args["type"] == "annuity" and not self.args["interest"]) or \ | |
len(list(filter(None.__ne__, self.args.values()))) < 4 or \ | |
any(n < 0 for n in [x for x in list(filter(None.__ne__, self.args.values())) if not isinstance(x, str)]): | |
print("Incorrect parameters.") | |
self.error = True | |
if not self.error: | |
if self.args["type"] == "diff": | |
self.principal = self.args["principal"] | |
self.periods = self.args["periods"] | |
self.interest = (self.args["interest"] / 100) / 12 | |
print(self.diff()) | |
else: | |
self.principal = self.args["principal"] | |
self.payment = self.args["payment"] | |
self.periods = self.args["periods"] | |
self.interest = (self.args["interest"] / 100) / 12 | |
if self.payment and self.principal: | |
msg = self.time() | |
elif self.payment and not self.principal: | |
msg = self.get_principal() | |
elif not self.payment and self.principal: | |
msg = self.annuity() | |
else: | |
msg = "Incorrect parameters." | |
print(msg) | |
def diff(self): | |
payments = [] | |
overpayment = 0 | |
for i in range(self.periods): | |
d = self.principal / self.periods + self.interest * (self.principal - (self.principal * i / self.periods)) | |
overpayment += ceil(d) | |
payments.append(f"Month {i+1}: paid out {ceil(d)}") | |
payments.append(f"\nOverpayment = {round(overpayment-self.principal)}") | |
return "\n".join(payments) | |
def time(self): | |
self.periods = ceil(log(self.payment / (self.payment - self.interest * self.principal), 1 + self.interest)) | |
overpayment = ceil(self.payment * self.periods - self.principal) | |
year = self.periods // 12 if ceil(self.periods % 12) < 12 else ceil(self.periods / 12) | |
months = ceil(self.periods % 12) if ceil(self.periods % 12) < 12 else 0 | |
formatted_time = f"{year} years{f' and {months} months' if months > 0 else ''}" | |
return f"You need {formatted_time} to repay this credit!\nOverpayment = : {overpayment}" | |
def get_principal(self): | |
self.principal = floor(self.payment / (self.interest * (1 + self.interest) ** self.periods / self.int())) | |
overpayment = (self.payment * self.periods) - self.principal | |
return f"Your credit principal = {self.principal}\nOverpayment = {overpayment}" | |
def annuity(self): | |
a = ceil(self.principal * ((self.interest * (1 + self.interest) ** self.periods) / self.int())) | |
return f"Your annuity payment = {a}!\nOverpayment = {(a * self.periods) - self.principal}" | |
def int(self): | |
return (1 + self.interest) ** self.periods - 1 | |
if __name__ == '__main__': | |
Credits() |
cyvax - 2024