#๐ cannot access local variable 'time' where it is not associated with a value
33 messages ยท Page 1 of 1 (latest)
@main sable
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Follow this instructions first
mb , done
you need time to be a global variable
You need to add global time in the start of the function
It's exactly as it says. You're trying to use time in a function but it was created outside of that function so it cannot be accessed.
but the for loop has the functions enclosed tho
So python know it's a global variable, not local variable
As the others have said, you can use a global variable.
A function can read a variable that is from another scope, but cannot write on it
And the for loop isn't an enclosure, but the function is
unless you specify it is the global one you use and not a new variable you create
That doesn't change anything. The time variable was made outside of the functions, so it cannot be accessed.
The scoping for variables in Python is very specific.
so if i enclose the for loop with func and then put the variables in the scope of the function , it will work
ah that makes sense
just use global
cant i just define the variables outside of the loop instead of bothering with global?
same problem
If they're defined out of the function you want to use them in, it won't work.
They must be either prefaced with global or initialised within the function.
or you can use arguments
Like for example
time = 0
def function_name(time):
blah
function_name(time)
then the variable inside the function will be different from the one outside even though they have the same name
Variables are scope-specific, so having the same name doesn't mean anything in different scopes.
!e
class A:
def __init__(self):
self.a = 0
a = A()
def fn1():
a # fine
def fn2():
global a
a # fine but not required
def fn3():
a = a # not fine: python detect variable overwrite, therefore `a` is local variable, but `a` doesn't exist in local
def fn4():
global a
a = a # fine: you told python it's global
def fn5():
a.a = a.a # fine: you are just reading `a` even tho you are overwriting `a.a`
for i,fn in enumerate([fn1,fn2,fn3,fn4,fn5], start=1):
try:
fn()
print(f"fn{i} fine")
except UnboundLocalError:
print(f"fn{i} not fine")
:white_check_mark: Your 3.13 eval job has completed with return code 0.
001 | fn1 fine
002 | fn2 fine
003 | fn3 not fine
004 | fn4 fine
005 | fn5 fine
Basically introduction to global, local scope
!e
class A:
def __init__(self):
self.a = 0
a = A()
def fn1():
a # not fine
a = A() # this makes python believe `a` is a local variable in compile time
def fn2():
a # not fine
if False:
a = A() # not fine: even if it never happen, how python decide it's local or global at compile time, so `a` is still local
def fn3():
a
a.a = a.a # fine: you are just reading `a` even tho you are overwriting `a.a`, so it can be global and isn't in local
def fn4():
a = A()
a.a = 1 # This only modify the local version of `a`, and doesn't affect global
def fn5():
print(a.a) # as seen, perfectly fine and still `0`
def fn6():
a.a = 1 # Since there are no local version of `a` and python haven't decided that it's local, you are editing the global version of `a`.
def fn7():
print(a.a) # Here you go this give 1
for i,fn in enumerate([fn1,fn2,fn3, fn4, fn5, fn6, fn7], start=1):
try:
fn()
print(f"fn{i} fine")
except UnboundLocalError:
print(f"fn{i} not fine")
:white_check_mark: Your 3.13 eval job has completed with return code 0.
001 | fn1 not fine
002 | fn2 not fine
003 | fn3 fine
004 | fn4 fine
005 | 0
006 | fn5 fine
007 | fn6 fine
008 | 1
009 | fn7 fine
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.