简体   繁体   中英

How can a function from different python module can be used in another defined function?

Here I want to use the c(v) & s(v) from a module FrenselIntegral.py in a defined function f(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()

But, the output is

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'

The point looks like fi.c(v) returns None. You can check what values of v give that result or just avoid None returns, for example:

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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