precio_accion
不是一个
list
或
dict
,这是一个浮点数,所以你不需要
precio_accion[0] = precio_accion
一点也不排队。如果你想要一个
列表
import math
import random
def funcion_gbm(pi = 100, media = 0.10, volatilidad = 0.05):
m = media
v = volatilidad
def funcion_anidada():
exponencial = math.exp((m - (1/2) * v**2) * (1/365) +
v * math.sqrt(1/365) * random.normalvariate(0, 1))
precio = pi * exponencial
return precio
return funcion_anidada
precio_accion = funcion_gbm()()
# Now precio_accion is a list and your iteration will work
precio_accion = [precio_accion]
编辑
target_price
没有改变,为什么要不断地重新定义它?您可以在循环之外定义它:
target_price = 125
for rueda in range(1,1000):
# Now you need to append to the list rather than trying to
# access other elements that aren't in the list yet
precio_accion.append(funcion_gbm(precio_accion[rueda - 1]))
# You're comparing the last item in the list every time, so
# you can just access it with -1
if precio_accion[-1] == target_price:
print("reached")
# No need for else since it's just continuing the loop