Trying to understand variable scope in python [duplicate]










1
















This question already has an answer here:



  • How to change a variable after it is already defined?

    6 answers



I gave this following program in Python just to understand scopes of variables, but I'm getting the error:



count = 0
def hello_func():
if(count == 5):
return 0
print("Hello")
count+=1
hello_func()

hello_func()


Error :



 UnboundLocalError: local variable 'count' referenced before assignment 


Can you explain what I'm doing wrong? And how do I declare count has global variable without altering the structure of the above program?










share|improve this question













marked as duplicate by Community Nov 11 '18 at 8:14


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






















    1
















    This question already has an answer here:



    • How to change a variable after it is already defined?

      6 answers



    I gave this following program in Python just to understand scopes of variables, but I'm getting the error:



    count = 0
    def hello_func():
    if(count == 5):
    return 0
    print("Hello")
    count+=1
    hello_func()

    hello_func()


    Error :



     UnboundLocalError: local variable 'count' referenced before assignment 


    Can you explain what I'm doing wrong? And how do I declare count has global variable without altering the structure of the above program?










    share|improve this question













    marked as duplicate by Community Nov 11 '18 at 8:14


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.




















      1












      1








      1









      This question already has an answer here:



      • How to change a variable after it is already defined?

        6 answers



      I gave this following program in Python just to understand scopes of variables, but I'm getting the error:



      count = 0
      def hello_func():
      if(count == 5):
      return 0
      print("Hello")
      count+=1
      hello_func()

      hello_func()


      Error :



       UnboundLocalError: local variable 'count' referenced before assignment 


      Can you explain what I'm doing wrong? And how do I declare count has global variable without altering the structure of the above program?










      share|improve this question















      This question already has an answer here:



      • How to change a variable after it is already defined?

        6 answers



      I gave this following program in Python just to understand scopes of variables, but I'm getting the error:



      count = 0
      def hello_func():
      if(count == 5):
      return 0
      print("Hello")
      count+=1
      hello_func()

      hello_func()


      Error :



       UnboundLocalError: local variable 'count' referenced before assignment 


      Can you explain what I'm doing wrong? And how do I declare count has global variable without altering the structure of the above program?





      This question already has an answer here:



      • How to change a variable after it is already defined?

        6 answers







      python






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 '18 at 8:10









      Maccen WrightMaccen Wright

      3311420




      3311420




      marked as duplicate by Community Nov 11 '18 at 8:14


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by Community Nov 11 '18 at 8:14


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
























          1 Answer
          1






          active

          oldest

          votes


















          0














          When using global variables within a function, you need to declare it explicitly:



          count = 0
          def hello_func():
          global count
          if(count == 5):
          return 0
          print("Hello")
          count+=1
          hello_func()

          hello_func()


          The global count in the third line declares I am using the global variable.






          share|improve this answer





























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            When using global variables within a function, you need to declare it explicitly:



            count = 0
            def hello_func():
            global count
            if(count == 5):
            return 0
            print("Hello")
            count+=1
            hello_func()

            hello_func()


            The global count in the third line declares I am using the global variable.






            share|improve this answer



























              0














              When using global variables within a function, you need to declare it explicitly:



              count = 0
              def hello_func():
              global count
              if(count == 5):
              return 0
              print("Hello")
              count+=1
              hello_func()

              hello_func()


              The global count in the third line declares I am using the global variable.






              share|improve this answer

























                0












                0








                0







                When using global variables within a function, you need to declare it explicitly:



                count = 0
                def hello_func():
                global count
                if(count == 5):
                return 0
                print("Hello")
                count+=1
                hello_func()

                hello_func()


                The global count in the third line declares I am using the global variable.






                share|improve this answer













                When using global variables within a function, you need to declare it explicitly:



                count = 0
                def hello_func():
                global count
                if(count == 5):
                return 0
                print("Hello")
                count+=1
                hello_func()

                hello_func()


                The global count in the third line declares I am using the global variable.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 11 '18 at 8:13









                DinariDinari

                1,619522




                1,619522













                    Popular posts from this blog

                    𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

                    Edmonton

                    Crossroads (UK TV series)