Python Forum

Full Version: NameError for global variable
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm defining a variable in a if statement which to my knowledge should be a global variable.

                    try:
                        for i in parts:
                            print(i)
                        if len(parts) == 3 and "PRIVMSG" in line:
                            message = parts[2][:len(parts[2]) - 1]
                            usernamesplit = string.split(parts[1], "!")
                        else:
                            if len(parts) == 4 and "PRIVMSG" in line:
                                message = parts[3][:len(parts[3]) - 1]
                                usernamesplit = sting.split(parts[2], "!")
                            else:
                                print(line)
                    except:
                        message = ""
                    username = usernamesplit[0]
it tells me usernamesplit is not defined on the last line. Even though I defined it a few lines above.

Here is the full code in case it matters. (sensitive information masked)

import socket, string, time
#connection variables
HOST = "irc.twitch.tv"
PORT = 6667
ID = "****"
AUTH = "oauth:****"
ROOM = "****"
readbuffer = ""

#function variables
MOTD = True


print("connecting")

s = socket.socket()
s.connect((HOST, PORT))
s.send("PASS " + AUTH + "\r\n")
s.send("NICK " + ID + "\r\n")
s.send("CAP REQ :twitch.tv/membership\r\n")
s.send("CAP REQ :twitch.tv/commands\r\n")
s.send("CAP REQ :twitch.tv/tags\r\n")
s.send("JOIN #" + ROOM + "\r\n")



def sendMessage(message):
    s.send("PRIVMSG #" + ROOM + " :" + message + "\r\n")
    print("Sent: " + message)


 #bot announces itself
print("HOST = " + HOST + ", PORT = " + str(PORT) + ", Account = " + ID + ", Channel = " + ROOM + ", STATUS : ONLINE")

while True:
    readbuffer = readbuffer + s.recv(1024)
    temp = string.split(readbuffer, "\n")
    readbuffer = temp.pop()

    for line in temp:
        #print(line)
        if "PING" in line:
            #print(line)
            sendMessage("/PONG")
        else:
            if "stopbot" in line:
                break
            else:
                parts = string.split(line, ":")
                if "QUIT" not in parts[1] and "JOIN" not in parts[1] and "PART" not in parts[1] and "USERSTATE" not in parts[1] and "ROOMSTATE" not in parts[1]:
                    try:
                        for i in parts:
                            print(i)
                        if len(parts) == 3 and "PRIVMSG" in line:
                            message = parts[2][:len(parts[2]) - 1]
                            usernamesplit = string.split(parts[1], "!")
                        else:
                            if len(parts) == 4 and "PRIVMSG" in line:
                                message = parts[3][:len(parts[3]) - 1]
                                usernamesplit = sting.split(parts[2], "!")
                            else:
                                print(line)
                    except:
                        message = ""
                    username = usernamesplit[0]


                    for l in parts:
                        if "End of /NAMES list" in l:
                            MOTD = False
                            print("MOTD ENDED")
my guess is that it's returning the else condition print(line). It's never set.
You're correct, it seems adding a bunch of try statements solved the issue.
Why do you have the try/except? The except will allow errors to be ignored. That's not a good idea. Now if there's any error anywhere in the block, it just goes to the end and doesn't say why it did so.

There are code paths that will bypass the assignment of usernamesplit and still reach the line where it is referenced.

* If both if sections are false (lines 4 and 8 in the first example), no block that sets the assignment will be executed
* If any error happens in the try block before usernamesplit is assigned, all other lines are bypassed (and the error is not printed).