Hello Stack Overflow Community,
I am trying to write a program that checks Fermat's Last Theorem. The issue I am running into is that an error appears saying "NameError: name 'a' is not defined. However, I define 'a' in my first function and return its value at the end of the function.
I am trying to use the inputed values from the first function in the second function so the user can define the parameters.
Am I misunderstanding how to leverage "Return"? All help is greatly appreciate and will keep me sane.
def input_fermat():
a=input('Enter the first variable \'a\': \n')
b=input('Enter the second variable \'b\': \n')
c=input('Enter the third variable \'c\': \n')
n=input('Enter the exponential variable \'n\': \n')
return a, b, c, n
def check_fermat(a,b,c,n):
calc_1=a**n
calc_2=b**n
calc_3=c**n
if n>2 and int(calc_1) + int(calc_2) == calc_3:
print('Holy smokes, Fermat was wrong!')
else:
print('No that doesn\'t work.')
input_fermat()
check_fermat(a,b,c,n)
You are not storing the values that the function
input_fermat
returns. Try:Returned values don't just automatically show up in your namespace, you have to assign them to something.
The variables
a, b, c, n
defined ininput_fermat
only exists within the function, that's why you return them, but when you call the function you aren't saving them anywhere. You should replace:By:
Or you can directly pass the return value of
input_fermat
tocheck_fermat
like this:This is happening because those variables are defined locally and are not available in check_fermat's namespace.
Refer to the LEGB-rule.
What you can do is define all of those variables using the global keyword in the function definition, although this isn't usually the best approach. You'll also want to cast all of your inputs to ints since input() will return a string.
Variables a,b,c and n, which you receive as input in
input_fermat()
, are only available within the body of that function; once you return, you're out ofinput_fermat()
's scope and the values in a,b,c and n are handed off to whatever variables you calledinput_fermat()
to assign .A function's scope means the only variables available in any given function are
In
check_fermat()
,this means you could re-use variables a,b,c and for something other than the input, if you wanted (because a new function means a new scope).But, in the code shown below, we decide that a,b,c and n in
check_fermat()
are going to be the same thing as a,b,c and d ininput_fermat()
with the declarationa,b,c,n = input_fermat()
. This is a decision we chose to make; it's arbitrary.Here's an edited version of your function that accomplishes what I think you were going for:
Note that because of the scopes of these functions, I could have declared the variables in
check_fermat()
as follows and it would all still work (try running this code for yourself to see)The Process of execution (for both code snippets) goes like this:
check_fermat()
on the last line is executed because it's the only function called (not just defined) in our .py file.check_fermat()
to execute it 3.Python findsinput_fermat()
is called insidecheck_fermat
and goes looking forinput_fermat()
s definition.check_fermat()
and is used to compute Fermat's Last Theorem.check_fermat()
is carried out (output is printed to terminal). Then,check_fermat()
returns None, ending the function call with no variables to return.