简体   繁体   English

为什么Python无法在我的模块中找到第二个函数?

[英]Why can't Python find the second function in my module?

Extreme Python newbie here. 极端Python新手在这里。 I'm trying to write some functions for the Black-Scholes option/greek formulas for LibreOffice Calc. 我正在尝试为LibreOffice Calc的Black-Scholes选项/希腊公式编写一些函数。 I wanted to make one big module with various functions I will need to use in a few spreadsheets. 我想制作一个具有各种功能的大模块,需要在一些电子表格中使用。 I have them saved in a file called TradingUtilities.py. 我将它们保存在一个名为TradingUtilities.py的文件中。 The first two functions look like, 前两个函数如下所示:

def BSCall(S, K, r, sig, T):
    import scipy.stats as sp
    import numpy as np
    d1 = (np.log(S/K) - (r - 0.5 * sig * sig) * T)/(sig*np.sqrt(T))
    d2 = d1 - sig*np.sqrt(T)
    P1 = sp.norm.cdf(d1)
    P2 = sp.norm.cdf(d2);
    call = S*P1 - K * np.exp(-r*T)*P2;
    return call
def BSPUt(S, K, r, sig, T):
    import scipy.stats as sp
    import numpy as np
    d1 = (np.log(S/K) - (r-0.5*sig*sig)*T)/(sig*np.sqrt(T))
    d2 = d1 - sig*np.sqrt(T)
    P1 = sp.norm.cdf(-d1)
    P2 = sp.norm.cdf(-d2)
    put = K*exp(-r*t)*P2 - S*P1
    return put

When I run the script from the command line, the first function works fine. 当我从命令行运行脚本时,第一个功能运行良好。 But I get the following error when I try to run the second, 但是当我尝试运行第二个时,出现以下错误,

>>> import TradingUtilities as tp
>>> tp.BSCall(238, 238.5, 0.05, 0.09, 0.09)
2.9860730330243541
>>> tp.BSPut(238, 238, 0.05, 0.09, 0.09)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'TradingUtilities' has no attribute 'BSPut'

I am trying to figure out what's wrong, but no luck so far. 我正在尝试找出问题所在,但到目前为止还没有运气。 If anyone can see what I am doing wrong, or point me in the right direction, I'd greatly appreciate it. 如果有人能看出我做错了什么,或指出正确的方向,我将不胜感激。

Thanks! 谢谢!

There is typo in your code 您的代码中有错别字

>>> tp.BSPut(238, 238, 0.05, 0.09, 0.09)

should be 应该

>>> tp.BSPUt(238, 238, 0.05, 0.09, 0.09)

Or you can change BSPUt to BSPut in main code. 或者,您可以在主代码BSPUt BSPut更改为BSPut

There is typo in your function name, compared to what you wrote in command line. 与您在命令行中编写的内容相比,您的函数名称中有错别字。 Python is case sensitive. Python区分大小写。

It should be: def BSPut(S, K, r, sig, T): 它应该是: def BSPut(S, K, r, sig, T):

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM