Accessing values defined inside functions

Hi, I have written the following code as part of a project to find the literature values for given constants (s1hkl and s2hkl) as well as these constants that I have found experimentally (s1hklBaye and s2hklBaye) where s1 and s2 are the values that have come out of earlier steps in my experiment. I have defined these equations as functions where I can pass through h,k and l which alter the values of the literature values and I have different experimental s1 and s2’s for different h k and l values. I would like to add code to find the percentage error between the expermental and litrature values which although in itself is quite simple, I can’t work out a way to access the values of s1hklBaye, s2hklBaye, s1hkl and s2hkl as these are inside the functions. I have left the code unedited for completeness so apologies if this makes things complicated. I would really appreciate any help

E=220*(109)
v=0.28
s1lit=(-v/E)*(10
12)
s2lit=((1+v)/E)*(10**12)
print("literature value for S1 = " + str(s1lit))
print("literature value for S2 = " + str(s2lit))

s1=-1.3
s2=6

def s1s2hklBaye(h,k,l):
Arx=1.49
Rhkl=((h2)*(k2)+(k2)*(l2)+(l2)*(h2)) / (((h2)+(k2)+(l**2))*2)
delta=(5
(Arx-1))/(3+2Arx)
print(“Arx=” + str(Arx),“R=” + str(Rhkl), “delta=” + str(delta))
s1hklBaye= s1-s2
(0.2Rhkl)delta
s2hklBaye= s2
(1+3
(0.2-Rhkl*delta))
print("Bayesian value for S1hkl = " + str(s1hklBaye))
print("Bayesian value for S2hkl = " + str(s2hklBaye))

def s1s2hkl(h,k,l):
Arx=1.49
Rhkl=((h2)*(k2)+(k2)*(l2)+(l2)*(h2)) / (((h2)+(k2)+(l**2))*2)
delta=(5
(Arx-1))/(3+2Arx)
print(“Arx=” + str(Arx),“R=” + str(Rhkl), “delta=” + str(delta))
s1hkl= s1lit-s2lit
(0.2Rhkl)delta
s2hkl= s2lit
(1+3
(0.2-Rhkl*delta))
print("literature value for S1hkl = " + str(s1hkl))
print("literature value for S2hkl = " + str(s2hkl))

s1s2hkl(1,1,0)
s1s2hklBaye(1,1,0)

Hello, @Python123, and welcome to Python Software Foundation Discourse!

When you post code within these discussions, please be sure to format it, so that important details such as indentation are displayed properly. One way to do this is to place lines of three back ticks before and after the code, as follows:


```
# example code
for i in range(10):
    print("Hello World!")
```

There is also a </> button at the top of the editing window for formatting code, after it has been selected.

Have you considered either declaring the variables global or making their values available outside the functions via return statements? If you would like to return multiple values, you can enclose them within a tuple.

We hope you enjoy the discussions here. You might be interested in taking a look at the following pages:

EDIT (February 19, 2022):

To return values in a tuple, you can use this as a final statement in the s1s2hklBaye function:

  return s1hklBaye, s2hklBaye

Then, outside the function, you can assign the returned values to variables, as follows:

s1Baye, s2Baye = s1s2hklBaye(1,1,0)