0

I want to define a function like this:

n_H  = 0.9 # constant . 
x     = np.array([16.7, 16.5, 16.1, 15, 13.5,14.2, 14.9, 13.7])# example to test
D      = np.array([0, 1, 4, 7, 16, 31, 64, 127])

Sec_nucli = []
x         = []
D         = []
Sec_nucli[i]=  n_H *D[i]* x[i]

where x, D are variables come from another functions, I put it as an array(just as a test here!!).
What I want is to make a for loop for the function Sec_nucli, that every time I use deferent value for x,D, and Sec_nucli. For that reson I put [i], infront of these variable(I dont know how can I write it)?!!. Could you please help me to write the function correctly?
Thank you for your help.

2
Contributors
3
Replies
33
Views
1 Week
Discussion Span
Last Post by ALosh99
0

I have tried this :

x     = np.array([16.7, 16.5, 16.1, 15, 13.5,14.2, 14.9, 13.7])# an example
D      = np.array([0, 1, 4, 7, 16, 31, 64, 127])# example
n_H = 0.9
Sec_nucli = []
x         = []
D         = []

for i in range(1,n): 
      Sec_nucli[i] =  n_H * D * xsec[i]
      sec_nucli += sec_nucli[i]
      return sec_nucli
## I defined the function like this:
def Fragmentation():
    n_H = 0.9
    Sec_nucli[i]=  n_H *D* x[i]
    return Sec_nucli
0

In this other way the code seems to work:

x = [16.7, 16.5, 16.1, 15, 13.5,14.2, 14.9, 13.7] # an example
D = [0, 1, 4, 7, 16, 31, 64, 127] # example
n_H = 0.9
Sec_nucli = 0

## I defined the function like this:
def Fragmentation(D,x):
    s_nucli = n_H * D * x
    return s_nucli

for xi in x: 
    for Dj in D:
      print(xi, Dj)
      Sec_nucli += Fragmentation(xi,Dj)
      print(Sec_nucli)
0

Thank you @xjr.If I want to add one parameter to the function and save the out put(sec_nucli) in a text file for example, I have try this:

Sec_nucli = 0
def Fragmentation(v,D,xsec):
    sec_nucli = v * n_H * D * xsec
    return sec_nucli

file=open("Secondary.txt", "a")     
for xi in xsec: 
    for Dj in D:
       print(xi, Dj)
     for vj in v:
       Sec_nucli += Fragmentation(vi,Dj, xi)
       print(Sec_nucli)
       s = str(Fragmentation(xi))
       file.write(s + "\n")
file.close()

Dose this correct!! .
Thank you very much for your help

Edited by ALosh99

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.