如何将来自不同python模块的功能用于另一个已定义的功能?

问题描述 投票:-1回答:1

这里我想在已定义的函数f(v)中使用模块FrenselIntegral.py中的c(v)和s(v)。

import FrenselIntegral as fi

v=0.0
u=0.2

def f(v): 0.5*((0.5-fi.c(v))**2+(0.5-fi.s(v))**2)

file=open("Straightedge diffraction pattern.txt","w")

for i in range (25):
   print>>file,v,f(v)
   v=v+u
file.close()

但是,输出是

Traceback (most recent call last):
  File "c:/Users/Shubhadeep/Desktop/New folder/Straightedge diffraction pattern.py", line 11, in <module>
    print>>file,v,f(v)
  File "c:/Users/Shubhadeep/Desktop/New folder/Straightedge diffraction pattern.py", line 4, in f
    def f(v): 0.5*((0.5-fi.c(v))**2+(0.5-fi.s(v))**2)
TypeError: unsupported operand type(s) for -: 'float' and 'NoneType'
python function python-2.6
1个回答
0
投票

该点看起来像fi.c(v)返回None。您可以检查v的哪些值给出该结果,或者只是避免返回None,例如:

import FrenselIntegral as fi

v=0.0
u=0.2

def f(v):
  if fi.c(v) is None or fi.s(v) is None:
    return 0
  else:
    return 0.5*((0.5-fi.c(v))**2+(0.5-fi.s(v))**2)

file=open("Straightedge diffraction pattern.txt","w")

for i in range (25):
   print>>file,v,f(v)
   v=v+u
file.close()
© www.soinside.com 2019 - 2024. All rights reserved.