Python: Return possibly not returning value





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















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)









share|improve this question





























    0















    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)









    share|improve this question

























      0












      0








      0








      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)









      share|improve this question














      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)






      python return syntax-error






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 17 '18 at 2:46









      AlexAlex

      12




      12
























          5 Answers
          5






          active

          oldest

          votes


















          2














          You are not storing the values that the function input_fermat returns. Try:



          a, b, c, n = input_fermat()

          check_fermat(a,b,c,n)





          share|improve this answer

































            1














            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.






            share|improve this answer































              1














              The variables a, b, c, n defined in input_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:



              input_fermat()


              By:



              a, b, c, n = input_fermat()


              Or you can directly pass the return value of input_fermat to check_fermat like this:



              check_fermat(*input_fermat())





              share|improve this answer


























              • Thanks for pointing it out Mark. I have to unpack the tupple first.

                – Constantine32
                Nov 17 '18 at 2:59



















              0














              Returned values don't just automatically show up in your namespace, you have to assign them to something.



              a, b, c, n = input_fermat()





              share|improve this answer































                0














                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 of input_fermat()'s scope and the values in a,b,c and n are handed off to whatever variables you called input_fermat() to assign .



                A function's scope means the only variables available in any given function are




                • those that are declared in the body of the function

                • those passed to the function as arguments in parentheses.

                • variables declared globally


                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 in input_fermat() with the declaration a,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:



                #Global variables would be declared up here, before all other function declarations.
                #Global variables would be available to all the functions that follow.

                def input_fermat(): #if there were arguments in these parentheses they'd be included in input_fermat scope
                # input_fermat() scope begins
                a=int(input('Enter the first variable 'a': n'))
                b=int(input('Enter the second variable 'b': n'))
                c=int(input('Enter the third variable 'c': n'))
                n=int(input('Enter the exponential variable 'n': n'))

                return a, b, c, n
                #input_fermat() scope ends

                def check_fermat(): #if there were arguments in these parentheses they'd be included in check_fermat scope
                #check_fermat() scope begins

                #just as you returned 4 variables at once in input_fermat(), 4 variables can be assigned at once here
                a,b,c,n = input_fermat() #need to assign because a, b, c, n from input_fermat() no longer in scope
                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')

                #python implicitly does a `return None` here
                #check_fermat() scope ends

                check_fermat()


                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)



                def input_fermat(): 
                a=int(input('Enter the first variable 'a': n'))
                b=int(input('Enter the second variable 'b': n'))
                c=int(input('Enter the third variable 'c': n'))
                n=int(input('Enter the exponential variable 'n': n'))

                return a, b, c, n

                def check_fermat():
                any,variable,names,go = input_fermat()
                calc_1=any**go
                calc_2=variable**go
                calc_3=name**go

                if n>2 and int(calc_1) + int(calc_2) == calc_3:
                print('Holy smokes, Fermat was wrong!')
                else:
                print('No that doesn't')

                check_fermat()


                The Process of execution (for both code snippets) goes like this:





                1. check_fermat() on the last line is executed because it's the only function called (not just defined) in our .py file.

                2. Python looks for the definition of check_fermat() to execute it
                  3.Python finds input_fermat() is called inside check_fermat and goes looking for input_fermat()s definition.

                3. Python finds the definition, executes the function and asks for input.

                4. Input is returned back to check_fermat() and is used to compute Fermat's Last Theorem.

                5. The rest of check_fermat() is carried out (output is printed to terminal). Then, check_fermat() returns None, ending the function call with no variables to return.






                share|improve this answer


























                  Your Answer






                  StackExchange.ifUsing("editor", function () {
                  StackExchange.using("externalEditor", function () {
                  StackExchange.using("snippets", function () {
                  StackExchange.snippets.init();
                  });
                  });
                  }, "code-snippets");

                  StackExchange.ready(function() {
                  var channelOptions = {
                  tags: "".split(" "),
                  id: "1"
                  };
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function() {
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled) {
                  StackExchange.using("snippets", function() {
                  createEditor();
                  });
                  }
                  else {
                  createEditor();
                  }
                  });

                  function createEditor() {
                  StackExchange.prepareEditor({
                  heartbeatType: 'answer',
                  autoActivateHeartbeat: false,
                  convertImagesToLinks: true,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader: {
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  },
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  });


                  }
                  });














                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function () {
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53347740%2fpython-return-possibly-not-returning-value%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  5 Answers
                  5






                  active

                  oldest

                  votes








                  5 Answers
                  5






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  2














                  You are not storing the values that the function input_fermat returns. Try:



                  a, b, c, n = input_fermat()

                  check_fermat(a,b,c,n)





                  share|improve this answer






























                    2














                    You are not storing the values that the function input_fermat returns. Try:



                    a, b, c, n = input_fermat()

                    check_fermat(a,b,c,n)





                    share|improve this answer




























                      2












                      2








                      2







                      You are not storing the values that the function input_fermat returns. Try:



                      a, b, c, n = input_fermat()

                      check_fermat(a,b,c,n)





                      share|improve this answer















                      You are not storing the values that the function input_fermat returns. Try:



                      a, b, c, n = input_fermat()

                      check_fermat(a,b,c,n)






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Feb 17 at 4:58

























                      answered Nov 17 '18 at 2:51









                      Lucas FariasLucas Farias

                      1851415




                      1851415

























                          1














                          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.






                          share|improve this answer




























                            1














                            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.






                            share|improve this answer


























                              1












                              1








                              1







                              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.






                              share|improve this answer













                              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.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 17 '18 at 3:02









                              mmusommuso

                              113




                              113























                                  1














                                  The variables a, b, c, n defined in input_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:



                                  input_fermat()


                                  By:



                                  a, b, c, n = input_fermat()


                                  Or you can directly pass the return value of input_fermat to check_fermat like this:



                                  check_fermat(*input_fermat())





                                  share|improve this answer


























                                  • Thanks for pointing it out Mark. I have to unpack the tupple first.

                                    – Constantine32
                                    Nov 17 '18 at 2:59
















                                  1














                                  The variables a, b, c, n defined in input_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:



                                  input_fermat()


                                  By:



                                  a, b, c, n = input_fermat()


                                  Or you can directly pass the return value of input_fermat to check_fermat like this:



                                  check_fermat(*input_fermat())





                                  share|improve this answer


























                                  • Thanks for pointing it out Mark. I have to unpack the tupple first.

                                    – Constantine32
                                    Nov 17 '18 at 2:59














                                  1












                                  1








                                  1







                                  The variables a, b, c, n defined in input_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:



                                  input_fermat()


                                  By:



                                  a, b, c, n = input_fermat()


                                  Or you can directly pass the return value of input_fermat to check_fermat like this:



                                  check_fermat(*input_fermat())





                                  share|improve this answer















                                  The variables a, b, c, n defined in input_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:



                                  input_fermat()


                                  By:



                                  a, b, c, n = input_fermat()


                                  Or you can directly pass the return value of input_fermat to check_fermat like this:



                                  check_fermat(*input_fermat())






                                  share|improve this answer














                                  share|improve this answer



                                  share|improve this answer








                                  edited Nov 17 '18 at 3:04

























                                  answered Nov 17 '18 at 2:51









                                  Constantine32Constantine32

                                  22118




                                  22118













                                  • Thanks for pointing it out Mark. I have to unpack the tupple first.

                                    – Constantine32
                                    Nov 17 '18 at 2:59



















                                  • Thanks for pointing it out Mark. I have to unpack the tupple first.

                                    – Constantine32
                                    Nov 17 '18 at 2:59

















                                  Thanks for pointing it out Mark. I have to unpack the tupple first.

                                  – Constantine32
                                  Nov 17 '18 at 2:59





                                  Thanks for pointing it out Mark. I have to unpack the tupple first.

                                  – Constantine32
                                  Nov 17 '18 at 2:59











                                  0














                                  Returned values don't just automatically show up in your namespace, you have to assign them to something.



                                  a, b, c, n = input_fermat()





                                  share|improve this answer




























                                    0














                                    Returned values don't just automatically show up in your namespace, you have to assign them to something.



                                    a, b, c, n = input_fermat()





                                    share|improve this answer


























                                      0












                                      0








                                      0







                                      Returned values don't just automatically show up in your namespace, you have to assign them to something.



                                      a, b, c, n = input_fermat()





                                      share|improve this answer













                                      Returned values don't just automatically show up in your namespace, you have to assign them to something.



                                      a, b, c, n = input_fermat()






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 17 '18 at 2:52









                                      Mark RansomMark Ransom

                                      228k30289512




                                      228k30289512























                                          0














                                          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 of input_fermat()'s scope and the values in a,b,c and n are handed off to whatever variables you called input_fermat() to assign .



                                          A function's scope means the only variables available in any given function are




                                          • those that are declared in the body of the function

                                          • those passed to the function as arguments in parentheses.

                                          • variables declared globally


                                          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 in input_fermat() with the declaration a,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:



                                          #Global variables would be declared up here, before all other function declarations.
                                          #Global variables would be available to all the functions that follow.

                                          def input_fermat(): #if there were arguments in these parentheses they'd be included in input_fermat scope
                                          # input_fermat() scope begins
                                          a=int(input('Enter the first variable 'a': n'))
                                          b=int(input('Enter the second variable 'b': n'))
                                          c=int(input('Enter the third variable 'c': n'))
                                          n=int(input('Enter the exponential variable 'n': n'))

                                          return a, b, c, n
                                          #input_fermat() scope ends

                                          def check_fermat(): #if there were arguments in these parentheses they'd be included in check_fermat scope
                                          #check_fermat() scope begins

                                          #just as you returned 4 variables at once in input_fermat(), 4 variables can be assigned at once here
                                          a,b,c,n = input_fermat() #need to assign because a, b, c, n from input_fermat() no longer in scope
                                          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')

                                          #python implicitly does a `return None` here
                                          #check_fermat() scope ends

                                          check_fermat()


                                          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)



                                          def input_fermat(): 
                                          a=int(input('Enter the first variable 'a': n'))
                                          b=int(input('Enter the second variable 'b': n'))
                                          c=int(input('Enter the third variable 'c': n'))
                                          n=int(input('Enter the exponential variable 'n': n'))

                                          return a, b, c, n

                                          def check_fermat():
                                          any,variable,names,go = input_fermat()
                                          calc_1=any**go
                                          calc_2=variable**go
                                          calc_3=name**go

                                          if n>2 and int(calc_1) + int(calc_2) == calc_3:
                                          print('Holy smokes, Fermat was wrong!')
                                          else:
                                          print('No that doesn't')

                                          check_fermat()


                                          The Process of execution (for both code snippets) goes like this:





                                          1. check_fermat() on the last line is executed because it's the only function called (not just defined) in our .py file.

                                          2. Python looks for the definition of check_fermat() to execute it
                                            3.Python finds input_fermat() is called inside check_fermat and goes looking for input_fermat()s definition.

                                          3. Python finds the definition, executes the function and asks for input.

                                          4. Input is returned back to check_fermat() and is used to compute Fermat's Last Theorem.

                                          5. The rest of check_fermat() is carried out (output is printed to terminal). Then, check_fermat() returns None, ending the function call with no variables to return.






                                          share|improve this answer






























                                            0














                                            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 of input_fermat()'s scope and the values in a,b,c and n are handed off to whatever variables you called input_fermat() to assign .



                                            A function's scope means the only variables available in any given function are




                                            • those that are declared in the body of the function

                                            • those passed to the function as arguments in parentheses.

                                            • variables declared globally


                                            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 in input_fermat() with the declaration a,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:



                                            #Global variables would be declared up here, before all other function declarations.
                                            #Global variables would be available to all the functions that follow.

                                            def input_fermat(): #if there were arguments in these parentheses they'd be included in input_fermat scope
                                            # input_fermat() scope begins
                                            a=int(input('Enter the first variable 'a': n'))
                                            b=int(input('Enter the second variable 'b': n'))
                                            c=int(input('Enter the third variable 'c': n'))
                                            n=int(input('Enter the exponential variable 'n': n'))

                                            return a, b, c, n
                                            #input_fermat() scope ends

                                            def check_fermat(): #if there were arguments in these parentheses they'd be included in check_fermat scope
                                            #check_fermat() scope begins

                                            #just as you returned 4 variables at once in input_fermat(), 4 variables can be assigned at once here
                                            a,b,c,n = input_fermat() #need to assign because a, b, c, n from input_fermat() no longer in scope
                                            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')

                                            #python implicitly does a `return None` here
                                            #check_fermat() scope ends

                                            check_fermat()


                                            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)



                                            def input_fermat(): 
                                            a=int(input('Enter the first variable 'a': n'))
                                            b=int(input('Enter the second variable 'b': n'))
                                            c=int(input('Enter the third variable 'c': n'))
                                            n=int(input('Enter the exponential variable 'n': n'))

                                            return a, b, c, n

                                            def check_fermat():
                                            any,variable,names,go = input_fermat()
                                            calc_1=any**go
                                            calc_2=variable**go
                                            calc_3=name**go

                                            if n>2 and int(calc_1) + int(calc_2) == calc_3:
                                            print('Holy smokes, Fermat was wrong!')
                                            else:
                                            print('No that doesn't')

                                            check_fermat()


                                            The Process of execution (for both code snippets) goes like this:





                                            1. check_fermat() on the last line is executed because it's the only function called (not just defined) in our .py file.

                                            2. Python looks for the definition of check_fermat() to execute it
                                              3.Python finds input_fermat() is called inside check_fermat and goes looking for input_fermat()s definition.

                                            3. Python finds the definition, executes the function and asks for input.

                                            4. Input is returned back to check_fermat() and is used to compute Fermat's Last Theorem.

                                            5. The rest of check_fermat() is carried out (output is printed to terminal). Then, check_fermat() returns None, ending the function call with no variables to return.






                                            share|improve this answer




























                                              0












                                              0








                                              0







                                              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 of input_fermat()'s scope and the values in a,b,c and n are handed off to whatever variables you called input_fermat() to assign .



                                              A function's scope means the only variables available in any given function are




                                              • those that are declared in the body of the function

                                              • those passed to the function as arguments in parentheses.

                                              • variables declared globally


                                              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 in input_fermat() with the declaration a,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:



                                              #Global variables would be declared up here, before all other function declarations.
                                              #Global variables would be available to all the functions that follow.

                                              def input_fermat(): #if there were arguments in these parentheses they'd be included in input_fermat scope
                                              # input_fermat() scope begins
                                              a=int(input('Enter the first variable 'a': n'))
                                              b=int(input('Enter the second variable 'b': n'))
                                              c=int(input('Enter the third variable 'c': n'))
                                              n=int(input('Enter the exponential variable 'n': n'))

                                              return a, b, c, n
                                              #input_fermat() scope ends

                                              def check_fermat(): #if there were arguments in these parentheses they'd be included in check_fermat scope
                                              #check_fermat() scope begins

                                              #just as you returned 4 variables at once in input_fermat(), 4 variables can be assigned at once here
                                              a,b,c,n = input_fermat() #need to assign because a, b, c, n from input_fermat() no longer in scope
                                              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')

                                              #python implicitly does a `return None` here
                                              #check_fermat() scope ends

                                              check_fermat()


                                              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)



                                              def input_fermat(): 
                                              a=int(input('Enter the first variable 'a': n'))
                                              b=int(input('Enter the second variable 'b': n'))
                                              c=int(input('Enter the third variable 'c': n'))
                                              n=int(input('Enter the exponential variable 'n': n'))

                                              return a, b, c, n

                                              def check_fermat():
                                              any,variable,names,go = input_fermat()
                                              calc_1=any**go
                                              calc_2=variable**go
                                              calc_3=name**go

                                              if n>2 and int(calc_1) + int(calc_2) == calc_3:
                                              print('Holy smokes, Fermat was wrong!')
                                              else:
                                              print('No that doesn't')

                                              check_fermat()


                                              The Process of execution (for both code snippets) goes like this:





                                              1. check_fermat() on the last line is executed because it's the only function called (not just defined) in our .py file.

                                              2. Python looks for the definition of check_fermat() to execute it
                                                3.Python finds input_fermat() is called inside check_fermat and goes looking for input_fermat()s definition.

                                              3. Python finds the definition, executes the function and asks for input.

                                              4. Input is returned back to check_fermat() and is used to compute Fermat's Last Theorem.

                                              5. The rest of check_fermat() is carried out (output is printed to terminal). Then, check_fermat() returns None, ending the function call with no variables to return.






                                              share|improve this answer















                                              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 of input_fermat()'s scope and the values in a,b,c and n are handed off to whatever variables you called input_fermat() to assign .



                                              A function's scope means the only variables available in any given function are




                                              • those that are declared in the body of the function

                                              • those passed to the function as arguments in parentheses.

                                              • variables declared globally


                                              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 in input_fermat() with the declaration a,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:



                                              #Global variables would be declared up here, before all other function declarations.
                                              #Global variables would be available to all the functions that follow.

                                              def input_fermat(): #if there were arguments in these parentheses they'd be included in input_fermat scope
                                              # input_fermat() scope begins
                                              a=int(input('Enter the first variable 'a': n'))
                                              b=int(input('Enter the second variable 'b': n'))
                                              c=int(input('Enter the third variable 'c': n'))
                                              n=int(input('Enter the exponential variable 'n': n'))

                                              return a, b, c, n
                                              #input_fermat() scope ends

                                              def check_fermat(): #if there were arguments in these parentheses they'd be included in check_fermat scope
                                              #check_fermat() scope begins

                                              #just as you returned 4 variables at once in input_fermat(), 4 variables can be assigned at once here
                                              a,b,c,n = input_fermat() #need to assign because a, b, c, n from input_fermat() no longer in scope
                                              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')

                                              #python implicitly does a `return None` here
                                              #check_fermat() scope ends

                                              check_fermat()


                                              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)



                                              def input_fermat(): 
                                              a=int(input('Enter the first variable 'a': n'))
                                              b=int(input('Enter the second variable 'b': n'))
                                              c=int(input('Enter the third variable 'c': n'))
                                              n=int(input('Enter the exponential variable 'n': n'))

                                              return a, b, c, n

                                              def check_fermat():
                                              any,variable,names,go = input_fermat()
                                              calc_1=any**go
                                              calc_2=variable**go
                                              calc_3=name**go

                                              if n>2 and int(calc_1) + int(calc_2) == calc_3:
                                              print('Holy smokes, Fermat was wrong!')
                                              else:
                                              print('No that doesn't')

                                              check_fermat()


                                              The Process of execution (for both code snippets) goes like this:





                                              1. check_fermat() on the last line is executed because it's the only function called (not just defined) in our .py file.

                                              2. Python looks for the definition of check_fermat() to execute it
                                                3.Python finds input_fermat() is called inside check_fermat and goes looking for input_fermat()s definition.

                                              3. Python finds the definition, executes the function and asks for input.

                                              4. Input is returned back to check_fermat() and is used to compute Fermat's Last Theorem.

                                              5. The rest of check_fermat() is carried out (output is printed to terminal). Then, check_fermat() returns None, ending the function call with no variables to return.







                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              edited Nov 17 '18 at 5:05

























                                              answered Nov 17 '18 at 4:32









                                              DavidDavid

                                              316




                                              316






























                                                  draft saved

                                                  draft discarded




















































                                                  Thanks for contributing an answer to Stack Overflow!


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid



                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.


                                                  To learn more, see our tips on writing great answers.




                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function () {
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53347740%2fpython-return-possibly-not-returning-value%23new-answer', 'question_page');
                                                  }
                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown







                                                  Popular posts from this blog

                                                  List item for chat from Array inside array React Native

                                                  Thiostrepton

                                                  Caerphilly