#๐Ÿ”’ cannot access local variable 'time' where it is not associated with a value

33 messages ยท Page 1 of 1 (latest)

lyric craneBOT
#

@main sable

Python help channel opened

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.

main sable
mortal geyser
#

Follow this instructions first

main sable
kindred notch
#

you need time to be a global variable

mortal geyser
#

You need to add global time in the start of the function

signal swallow
# main sable mb , done

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.

main sable
#

but the for loop has the functions enclosed tho

mortal geyser
#

So python know it's a global variable, not local variable

signal swallow
#

As the others have said, you can use a global variable.

kindred notch
#

A function can read a variable that is from another scope, but cannot write on it

mortal geyser
kindred notch
#

unless you specify it is the global one you use and not a new variable you create

signal swallow
#

The scoping for variables in Python is very specific.

main sable
main sable
kindred notch
#

just use global

main sable
kindred notch
#

same problem

signal swallow
#

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)
rocky willow
signal swallow
#

Variables are scope-specific, so having the same name doesn't mean anything in different scopes.

mortal geyser
#

!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")

lyric craneBOT
mortal geyser
#

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")

lyric craneBOT
#
Python help channel closed using Discord native close action

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.