Skip to content

Instantly share code, notes, and snippets.

@andreferretti
Last active May 6, 2022 01:56
Show Gist options
  • Select an option

  • Save andreferretti/3780a67920334c750ce819cb30fb42d9 to your computer and use it in GitHub Desktop.

Select an option

Save andreferretti/3780a67920334c750ce819cb30fb42d9 to your computer and use it in GitHub Desktop.
from time import sleep
print ("Welcome to the DCF calculator.")
def ask_perpetuity():
earnings = float(input("Enter last twelve months earnings: "))
discount_rate = float(input("Enter discount rate in %: "))/100
growth_rate = float(input("Enter future expected annual compounded growth rate in %: "))/100
growth_period = int(input("Enter amount of future years of growth, after which earnings will flatten: "))
return discount_rate, earnings, growth_rate, growth_period
def calculate_perpetuity(discount_rate, earnings, growth_rate, growth_period):
dcf = 0
for years in range(growth_period):
years += 1
dcf += (earnings * (((1 + growth_rate) / (1 + discount_rate)) ** years)) # Adds up to end of growth
dcf += earnings * (((1 + growth_rate) / (1 + discount_rate)) ** growth_period) / discount_rate # to forever
dcf = str(dcf)
print("Calculating...")
sleep(1)
print("The discount cash flow is " + dcf)
def check_perpetuity(discount_rate, earnings, growth_rate, growth_period):
if 0 <= discount_rate <= 1 and earnings >= 0 and -1 <= growth_rate <= 1 and growth_period >= 0:
calculate_perpetuity(discount_rate, earnings, growth_rate, growth_period)
else:
wrong_input()
def ask_annuity():
earnings = float(input("Enter value of periodic payment: "))
discount_rate = float(input("Enter discount rate in %: ")) / 100
period = int(input("Enter period of years annuity will be received: "))
return discount_rate, earnings, period
def calculate_annuity(discount_rate, earnings, period):
dcf = 0
for years in range(period):
years += 1
dcf += (earnings / (1 + discount_rate) ** years) # This sum adds values to dcf up to end of period
dcf = str(dcf)
print("Calculating...")
sleep(1)
print("The discount cash flow is " + dcf)
def check_annuity(discount_rate , earnings, period):
if 0 <= discount_rate <= 1 and earnings >= 0 and period >= 0:
calculate_annuity(discount_rate, earnings, period)
else:
wrong_input()
def wrong_input():
print("\nYou did not enter valid inputs. Please retry. ")
ask_user()
def ask_user():
user_choice = input("Enter P to calculate perpetuity, A for annuity: ").upper()
if user_choice == "P":
discount_rate, earnings, growth_rate, growth_period = ask_perpetuity()
check_perpetuity(discount_rate, earnings, growth_rate, growth_period)
elif user_choice == "A":
discount_rate, earnings, period = ask_annuity()
check_annuity(discount_rate, earnings, period)
else:
wrong_input()
# Commands start here
ask_user()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment