0
i = float (raw_input("Enter i"))
Pd = int (raw_input("Enter Pd"))

while True:
    P1= (i-6.95)/(2*9.68*0.0001)
    P2= (i-7.051)/(2*7.378*0.0001)
    P3= (i-6.531)/(2*1.04*0.001)
    e= Pd-P1-P2-P3

    if e<=1 :
       F1=9.68*0.0001*P1*P1 + 6.95*P1 + 749.55
       F2=7.738*0.0001*P2*P2 + 7.051*P2 + 1285
       F3=1.04*0.001*P3*P3 + 6.531*P3 + 1531
       F= F1+F2+F3
       print 'Total cost F is {0}\n'.format(F)
       print P1
       print P2
       print P3
       break
    else :           
       i=i + 0.1(i)

I wrote a simple while loop like this to calculate the power demand and generation. to entre a power demand Pd #and incremental cost. I can calculate each power generator output P1 P2 and P3. there is a iteration required #which is when Pd-the sum of P1 P2 and P3, should be less than one.

when I run it by inputting i=8.5 and pd=2500, the results are 800.619834711981.973434535, 946.634615385. it means #the thing never iterate since the sum of this three are not 2500.

can someone tell me why is not iterating and what is wrong with my while true loop.

4

1 回答 1

1

e 的值在第一次迭代时是 -229.22788463,所以你要跳出循环。

来自 Python控制流文档:

与 C 中的break语句一样,该语句打破了最小的封闭forwhile循环。

根据您的问题:

需要一个迭代#这是当 Pd-P1 P2 和 P3 的总和应该小于 1 时。

我认为您想通过以下方式计算 e:

e = Pd - (P1 + P2 + P3)

不过还不清楚。也许你只是希望你的 if 语句是:

if (P1 + P2 + P3) < Pd:
于 2013-03-24T03:57:06.777 回答