van.py

Created by matthieumorvant

Created on January 13, 2024

1.27 KB


def calculate_npv(rate, cash_flows):
    npv = 0
    for i in range(len(cash_flows)):
        npv += cash_flows[i] / ((1 + rate) ** i)
    return npv

def calculate_irr(cash_flows):
    rate, increment = 0.01, 0.0001
    npv = calculate_npv(rate, cash_flows)
    while npv > 0:
        rate += increment
        npv = calculate_npv(rate, cash_flows)
    return rate

def financial_metrics():
    cash_flows = []
    rate = float(input("Taux : "))
    initial_investment = float(input("Invest. init. : "))
    cash_flows.append(-initial_investment)
    years = int(input("Nb d'années : "))
    
    for i in range(1, years + 1):
        cf = float(input("Flux année " + str(i) + " : "))
        cash_flows.append(cf)
    
    npv = calculate_npv(rate, cash_flows)
    irr = calculate_irr(cash_flows)
    
    print("VAN : " + str(npv))
    print("TIR : " + str(irr))

    # Calcul et affichage des flux actualisés et cumulés
    cumulative_cash_flow = 0
    for i in range(len(cash_flows)):
        discounted_cf = cash_flows[i] / ((1 + rate) ** i)
        cumulative_cash_flow += discounted_cf
        print("Flux actualisé année " + str(i) + " : " + str(discounted_cf))
        print("Cumulé jusqu'à année " + str(i) + " : " + str(cumulative_cash_flow))

financial_metrics()

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.