0

I have to figure out the time taken for one of my inputs to triple using a compound interest equation. The users have to input the expected annual return and the initial stock price, and the code is supposed to spit out the number of years (rounded up to whole number) it takes for that initial stock price to triple. However, with the code I've written it simply gives me nothing, and I have no idea what to do.

My code:

from sympy.solvers import solve
from sympy import Symbol


x = Symbol('x')
Stock_Ticker = input("Enter the stock ticker: ")
EAR = float(input("Enter Expected Annual Return: "))
ISP = float(input("Enter initial stock price: "))

expr = ISP * (1+EAR)**x

solve(ISP *(1+EAR)**x,x)

sol = solve(ISP *(1+EAR)**x,x)

print ("The price of", Stock_Ticker, "tripled after", sol, "years")

My output is:

The price of (Stock_Ticker) tripled after [] years
dspencer
  • 4,297
  • 4
  • 22
  • 43

1 Answers1

0

Issue

  • Expression sol = solve(ISP *(1+EAR)**x,x) never goes to zero.
  • You want sol = solve(ISP *(1+EAR)**x,x) - 3*ISP which goes to zero when value becomes 3*ISP

Code

from sympy.solvers import solve
from sympy import Symbol

x = Symbol('x')
Stock_Ticker = input("Enter the stock ticker: ")
EAR = float(input("Enter Expected Annual Return: "))
ISP = float(input("Enter initial stock price: "))

expr = ISP * (1+EAR)**x - 3*ISP   # subtract 3*initial price since we want to find when expression becomes zero

sol = solve(expr, x)              # Find root (zero) of expression expr

print ("The price of", Stock_Ticker, "tripled after", sol, "years")

Test Run

Enter the stock ticker: xxxx
Enter Expected Annual Return: .1
Enter initial stock price: 1
The price of xxxx tripled after [11.5267046072476] years
DarrylG
  • 16,732
  • 2
  • 17
  • 23