Python-init-yu问题:必须使用Bank-instance作为第一个参数来调用unbound方法yu init_uUi()(改为使用int-instance)

2024-10-01 15:37:18 发布

您现在位置:Python中文网/ 问答频道 /正文

class Teller(object):
    def __init__(self):
        self.occupied = False
        self.timeLeft = 0
        self.totTime

    def occupy(self, timeOcc):
        self.occupied = True
        self.timeLeft = timeOcc

    def nextMin(self):
        self.timeLeft -= 1
        self.totTime += 1
        if self.timeLeft == 0:
            self.occupied = False

class Bank(object):
    def __init__(numTellers, hoursOpen):
        self.tellers = []
        self.timeWaited = 0
        self.clientsWaiting = []
        for x in xrange(numTellers):
            tempTeller = Teller.__init__()
            self.tellers.append(tempTeller)
        self.minutesOpen = hoursOpen * 60

    def tellerOpen(self):
        for x in xrange(len(self.tellers)):
            if not self.tellers[x].occupied:
                return x+1
        return 0

    def runSim(self, queueInput):  #queueInput is a list of tuples (time, timeAtTeller)
        simTime = self.minutesOpen
        totCli = 0
        timeToNext = queueInput[0][0]
        timeAtNext = queueInput[0][1]
        queueInput.pop(0)
        self.clientsWaiting.append([timeToNext, timeAtNext])

        while simTime > 0:
            for person in self.clientsWaiting:
                if person[0]:
                    person -= 1
            if not self.clientsWaiting[len(self.clientsWaiting)-1][0]:
                timeToNext = queueInput[0][0]
                timeAtNext = queueInput[0][1]
                queueInput.pop(0)
                self.clientsWaiting.append([timeToNext, timeAtNext])

            remove = 0
            for x in xrange (len(self.clientsWaiting)-1):
                if tellerOpen() and not self.clientsWaiting[x][0]:
                    self.tellers[tellerOpen()].occupy(self.clientsWaiting[x][0])
                    totCli += 1
                    remove += 1
                elif not tellerOpen() and not self.clientsWaiting[x][0]:
                    self.timeWaited += 1

            for x in xrange(remove):
                self.clientsWaiting.pop(x)

            print """The total time spent in the queue by all clients was %d minutes. The total number of clients today was %d. The average waiting time was %d mins""" % (self.timeWaited, totCli, self.timeWaited / totCli)\



    if __name__ == '__main__':
    inp = raw_input()
    tList = inp.split('\n')
    qList = []
    for item in tList:
        tList = item.split(' ')
        qList.append((tList[0], tList[1]))

    virtBank = Bank.__init__(3, 7)
    bank.runSim(qList)

这将导致以下错误:

^{pr2}$

我不知道我有什么错。如有任何建议,将不胜感激。在

我认为,唯一重要的部分是Bank class __init__和调用{}


Tags: inselfforifinitdefnotxrange
1条回答
网友
1楼 · 发布于 2024-10-01 15:37:18

这里有两点:

  1. 您不应该直接调用__init__,这是一个神奇的方法,当您构造这样一个对象时会调用它:

    virtBank = Bank(3, 7)
    
  2. 实例隐式传递给构造函数,但必须显式接收,如下所示:

    ^{2美元

相关问题 更多 >

    热门问题