由于某种原因,我在这里出现global name is not defined错误。问题出在addClient方法中,我在其中增加了全局变量joinID。它向我抛出错误NameError: global name 'joinID' is not defined。我究竟做错了什么?

class Chatroom:
    clients = []
    joinID = 0

    def __init__(self,name,refNum):
        self.refNum = refNum
        self.name = name

    def addClient(self,clientName):
        global clients
        global joinID
        joinID = joinID+1
        clients.append(clientName, joinID)

    def removeClient(self, clientName, joinID):
        global clients
        clients.remove(clientName, joinID)

最佳答案

把变量放在课外

joinID=0
clients=[]
class Chatroom:
    def __init__(self,name,refNum):

09-04 22:04