0

I've looked around and don't really know what else to do. My function is not defined after the place where I call it. I'm thinking its probably something stupid, but I can't find it.

def function1():
    global tobegrouped
    if(len(tobegrouped) >= 2): 
        print(len(tobegrouped)) 
        prs1 = random.choice(tobegrouped)
        print("got prs1")
        prs2 = random.choice(tobegrouped)
        print("got prs2")
        newgroup = group(prs1, prs2)
        print("made group")
        global groups
        groups.append(newgroup)
        print("appended to group")
        newgroup.send_message("Welcome to robinbot, have fun, and don't spam", self)
    else :
        print("no group ready yet")

And this is where I'm calling it. I've already checked that its past the definition of the function in the file.

if command == '/start':
        #   MAYBE CHECK IF IN GROUP HERE
        global tobegrouped
        tobegrouped.append(chat_id)
        print("in to be grouped")
        self.sendMessage(chat_id, "welcome to robin, please wait to be grouped")
        print("sent message")
        function1()
        print("function1s working")

                              

And here is the error just in case you need it

enter image description here

EDIT: Error as text

File "bot.py", line 133, in on_chat_message

function1()

NameError: name 'function1' is not defined

Traceback (most recent call last):

File "bot.py" line 223, in (module)

time.sleep(10)

Community
  • 1
  • 1
ThatsATen
  • 1
  • 1
  • 2
  • 1
    Could you take the trouble to copy and paste (or carefully type, if necessary) the complete error in your question? It'll make it easier to see what's going on. – alexis May 11 '16 at 14:09
  • It does not matter whether it's past the definition of the function in the file, but whether the definition of the function is executed before you use it. It could e.g. be defined within an if-block or in another function. – tobias_k May 11 '16 at 14:11
  • There is very clearly something else going on here since, your error message places the `NameError: ...` **before other lines of the Traceback.** how is it shown without halting the program? – Tadhg McDonald-Jensen May 11 '16 at 14:22

1 Answers1

0

The error occurs on line 133, presumably inside some function that is ultimately called from line 223 (if I'm interpreting your snippet correctly). Clearly, function1() is not defined at the time that your code reaches line 223.

If function1() appears before line 223 in the same file, it must be defined inside another function (or possibly a class) that limits the scope of its definition. Or possibly the definition is inside an if block that comes out False, or some such. Python function definitions are ordinary code, so it's possible to go past them without executing them.

alexis
  • 48,685
  • 16
  • 101
  • 161