Python 3.6: NameError: name 'A' is not defined
Python 3.6: NameError: name 'A' is not defined
I'm very new to Python (3.6). My console is giving me the following error: "NameError: name 'A' is not defined". How can I fix this error?
def NuclearBindingEnergy(A,Z):
a_V=15.67
a_S=17.23
a_C=0.75
a_A=93.2
a_P=0
if A%2==1:
a_P=0
else:
if Z%2==0:
a_P=12.0
else:
a_P=-12.0
B = (a_V)*A - ((a_S)*(A**(2/3))) - (a_C)*(Z**2/(A**(1/3))) - ((a_A)*(((A-(2*Z))**2)/A)) + ((a_P)/A**(1/2))
return B
def NuclearBindingEnergyPerNucleon(A,Z):
return NuclearBindingEnergy(A,Z)/A
print(NuclearBindingEnergy(A,Z))
print(NuclearBindingEnergy(A,Z)/A)
You should define A and Z in the end two lines because you used them without defining them outside thier function.
– Azhy
Sep 17 '18 at 2:17
2 Answers
2
you have to declare A and Z variables values for example as following
A =12
Z = 5
print(NuclearBindingEnergy(A,Z))
print(NuclearBindingEnergy(A,Z)/A)
it will output
55.008666416854204
4.584055534737851
I had left out some code and spotted it - thanks for your response.
– NotEinstein
Sep 17 '18 at 2:31
When you use parameters in a function you dont need to define them because they must be defined when the function used, but when you use some function and you want to take some values inside them as a parameter so you must defined them before using in the functions parameter so in your example you just can define A and Z like :-
...
A = 20
Z = 30
print(NuclearBindingEnergy(A,Z))
print(NuclearBindingEnergy(A,Z)/A)
...
And also you can use numbers instead them like:-
...
print(NuclearBindingEnergy(70, 60))
print(NuclearBindingEnergy(80,90)/100)
...
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy
What are A and Z in your print statements at the bottom?
– Loocid
Sep 17 '18 at 2:16