Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8391895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:22:38+00:00 2026-06-09T19:22:38+00:00

test1 = 0 def test_func(): test1 += 1 test_func() I am receiving the following

  • 0
test1 = 0
def test_func():
    test1 += 1
test_func()

I am receiving the following error:

UnboundLocalError: local variable ‘test1’ referenced before assignment.

Error says that 'test1' is local variable but i thought that this variable is global

So is it global or local and how to solve this error without passing global test1 as argument to test_func?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T19:22:40+00:00Added an answer on June 9, 2026 at 7:22 pm

    In order for you to modify test1 while inside a function you will need to do define test1 as a global variable, for example:

    test1 = 0
    def test_func():
        global test1 
        test1 += 1
    test_func()
    

    However, if you only need to read the global variable you can print it without using the keyword global, like so:

    test1 = 0
    def test_func():
        print(test1)
    test_func()
    

    But whenever you need to modify a global variable you must use the keyword global.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get this error object has no attribute 'im_func' with this class Test(object): def
I have codes just like follows: class test: def do_something(): pass test1 = test()
The following code: class MyClass(): def test(self): self.__x = 0 def __setattr__(self, name, value):
How can it be that this test case import unittest class PropTest(unittest.TestCase): def test(self):
class Test: def func(): print('func') test1 = Test() test2 = Test() test1.func() #TypeError: fun1()
Try running the following code: class Test(object): def func_accepting_args(self,prop,*args): msg = %s getter/setter got
I expected the following to ouput 1 2 GSP <test:test1> <test:test2> ${test1} ${test2} </test:test2>
I have some questions regarding the following code: 1 class Test(object): 2 def __init__(self):
With the following code: class ObjA def func puts ObjA end end module Mod
If I have the following function: def intercept(func): # do something here @intercept(arg1=20) def

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.