我试图创建一个模拟,其中有两台打印机,我找到每台打印机的平均等待时间。我在我的程序中使用了一个类用于打印机和任务。基本上,我将每个模拟的等待时间添加到列表并计算平均时间。我的问题是我得到一个除以0的错误,所以没有附加任何内容。当我尝试使用1台打印机(基本上是同一件事)时,我没有任何问题。这是我第二台打印机的代码。我正在使用队列。
if printers == 2:
for currentSecond in range(numSeconds):
if newPrintTask():
task = Task(currentSecond,minSize,maxSize)
printQueue.enqueue(task)
if (not labPrinter1.busy()) and (not labPrinter2.busy()) and \
(not printQueue.is_empty()):
nexttask = printQueue.dequeue()
waitingtimes.append(nexttask.waitTime(currentSecond))
labPrinter1.startNext(nexttask)
elif (not labPrinter1.busy()) and (labPrinter2.busy()) and \
(not printQueue.is_empty()):
nexttask = printQueue.dequeue()
waitingtimes.append(nexttask.waitTime(currentSecond))
labPrinter1.startNext(nexttask)
elif (not labPrinter2.busy()) and (labPrinter1.busy()) and \
(not printQueue.is_empty()):
nexttask = printQueue.dequeue()
waitingtimes.append(nexttask.waitTime(currentSecond))
labPrinter2.startNext(nexttask)
labPrinter1.tick()
labPrinter2.tick()
averageWait = sum(waitingtimes)/len(waitingtimes)
outfile.write("Average Wait %6.2f secs %3d tasks remaining." \
%(averageWait,printQueue.size()))
任何帮助都会很棒!
编辑:我应该提到,无论价值观如何,都会发生这种情况。我可以有99-100的页面范围和1的PPM,但我仍然被0除。
答案 0 :(得分:0)
我认为你的问题源于第一次迭代时的空 string cmd = "select * from Pics where palbum= @album and pcat=@cat";
SqlDataAdapter adp = new SqlDataAdapter(cmd, con);
adp.Parameters.AddWithValue("@album", Request.QueryString["album"]);
adp.Parameters.AddWithValue("@cat", Request.QueryString["cat"]);
DataTable dt = new DataTable();
adp.Fill(dt);
Repeater1.DataSource = dt;
Repeater1.DataBind();
左右。如果队列中没有打印作业,并且从未插入等待时间,您将使用waitingtimes
(空)到达循环的底部,然后执行:
waitingtimes==[]
将是
sum(waitingtimes) / len(waitingtimes)
哪个是
sum([]) / len([])
解决这个问题的最简单方法就是检查它,或者抓住它:
0 / 0
或者:
if not waitingtimes:
averageWait = 0
else:
averageWait = sum(waitingtimes)/len(waitingtimes)