Python Forum
NameError for global variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NameError for global variable
#1
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")
Reply
#2
my guess is that it's returning the else condition print(line). It's never set.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
You're correct, it seems adding a bunch of try statements solved the issue.
Reply
#4
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).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  It's saying my global variable is a local variable Radical 5 1,154 Oct-02-2023, 12:57 AM
Last Post: deanhystad
  Variable scope - "global x" didn't work... ptrivino 5 3,033 Dec-28-2020, 04:52 PM
Last Post: ptrivino
  Spyder Quirk? global variable does not increment when function called in console rrace001 1 2,206 Sep-18-2020, 02:50 PM
Last Post: deanhystad
  Function Recognises Variable Without Arguments Or Global Variable Calling. OJGeorge4 1 2,239 Apr-06-2020, 09:14 AM
Last Post: bowlofred
  Global Variable Not Updating joew 2 7,816 Jan-26-2020, 04:15 PM
Last Post: joew
  Help with Global/Coerced Variable (Understanding Scope) Rev2k 6 3,481 Jan-09-2020, 03:43 AM
Last Post: Rev2k
  NameError: NameError: global name 'BPLInstruction' is not defined colt 7 4,379 Oct-27-2019, 07:49 AM
Last Post: Larz60+
  Declaring a Global Variable in a Function Bob1948 4 3,022 Sep-14-2019, 11:16 PM
Last Post: ichabod801
  Global variable does not seem to be global. Columbo 6 3,672 Jul-15-2019, 11:00 PM
Last Post: Columbo
  change value of a global variable across all modules aster 5 4,996 Jan-01-2019, 06:42 PM
Last Post: aster

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020