Beginner Python: Where to "while"?
tl;dr: My code "works", in that it gives me the answer I need. I just
can't get it to stop running when it reaches that answer. I'm stuck with
scrolling back through the output.
I'm a complete novice at programming/Python. In order to hone my skills, I
decided to see if I could program my own "solver" for Implied Equity Risk
Premium from Prof. Damodaran's Valuation class. Essentially, the code
takes some inputs and "guesses and tests" a series of interest rates until
it gets a "close" value to the input.
Right now my code spits out an output list, and I can scroll back through
it to find the answer. It's correct. However, I cannot for the life of me
get the code to "stop" at the correct value with the while function.
I have the following code:
per = int(input("Enter the # of periods forecast ->"))
divbb = float(input("Enter the initial dividend + buyback value ->"))
divgr = float(input("Enter the div + buyback growth rate ->"))
tbondr = float(input("Enter the T-Bond rate ->"))+0.000001
sp = int(input("Enter the S&P value->"))
total=0
pv=0
for i in range(1,10000):
erp = float(i/10000)
a = divbb
b = divgr
pv = 0
temppv = 0
print (sp-total, erp)
for i in range(0, per):
a=a * (1+b)
temppv = a / pow((1+erp),i)
pv=pv+temppv
lastterm=(a*1+tbondr)/((erp-tbondr)*pow(1+erp,per))
total=(pv+lastterm)
From his example, with the inputs:
per = 5
divbb = 69.46
divgr = 0.0527
tbondr = 0.0176
sp = 1430
By scrolling back through the output, I can see my code produces the
correct minimum at epr=0.0755.
My question is: where do I stick the while to stop this code at that
minimum? I've tried a lot of variations, but can't get it. What I'm
looking for is, basically:
while (sp-total) > |1|, keep running the code.
No comments:
Post a Comment