追加到列表时对象不可迭代

时间:2018-10-28 18:22:05

标签: python-2.7 object append typeerror

我正在为一个数据结构类设计一个项目,但是在列出多个收银员的列表时遇到了麻烦。

from cashier import Cashier
from customer import Customer

class MarketModel(object):

def __init__(self, lengthOfSimulation, averageTimePerCus,
             probabilityOfNewArrival, numberOfCashiers):
    self._probabilityOfNewArrival = probabilityOfNewArrival
    self._lengthOfSimulation = lengthOfSimulation
    self._averageTimePerCus = averageTimePerCus
    self._cashierList = []
    for i in range(numberOfCashiers):
        self._cashierList.append(Cashier())

然后是使用MarketModel的文件,其中收银员数量固定为4:

def main():
    print "Welcome to the Martket Simulator!"
    lengthOfSimulation = input("Enter the total running time: ")
    averageTimePerCus = input("Enter the average time per customer: ")
    probabilityOfNewArrival = input("Enter the probability of a new arrival: ")
    if lengthOfSimulation < 1 or lengthOfSimulation > 1000:
        print "Running time must be an integer greater than 0" + \
            "\nand less than or equal to 1000"
    elif averageTimePerCus <= 0 or averageTimePerCus >= lengthOfSimulation:
        print "Average time per customer must be an integer" + \
            "\ngreater than 0 and less than running time"
    elif probabilityOfNewArrival <= 0 or probabilityOfNewArrival > 1:
        print "Probability must be geater than 0" + \
            "\nand less than or equal to 1"
    else:
        model = MarketModel(lengthOfSimulation, averageTimePerCus,
                            probabilityOfNewArrival,4) 
        print model.runSimulation()

运行此代码时出现此错误:

     16         for i in range(numberOfCashiers):
 ---> 17             self._cashierList.append(Cashier())
     18 
     19     def runSimulation(self):
 TypeError: 'Cashier' object is not iterable 

据我所知,当我尝试在主函数中初始化MarketModel对象时,当我尝试创建收银员列表时,它会卡住。我认为没有必要将事物添加到列表中。这是怎么回事?

0 个答案:

没有答案