简体   繁体   English

关于while真实循环

[英]about the while true loop

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. 我写了一个这样的简单while循环来计算功率需求和发电量。 to entre a power demand Pd #and incremental cost. 输入电力需求Pd#和增量成本。 I can calculate each power generator output P1 P2 and P3. 我可以计算出每个发电机的输出P1,P2和P3。 there is a iteration required #which is when Pd-the sum of P1 P2 and P3, should be less than one. #需要进行一次迭代,即当Pd-P1 P2和P3之和小于1时。

when I run it by inputting i=8.5 and pd=2500, the results are 800.619834711981.973434535, 946.634615385. 当我通过输入i = 8.5和pd = 2500运行它时,结果为800.619834711981.973434535,946.634615385。 it means #the thing never iterate since the sum of this three are not 2500. 这意味着#事情永远不会迭代,因为这三个的总和不是2500。

can someone tell me why is not iterating and what is wrong with my while true loop. 有人可以告诉我为什么不迭代以及我的while true循环出了什么问题。

The value of e is -229.22788463 on the first iteration, so you're breaking out of the loop. e的值在第一次迭代中为-229.22788463,因此您正在摆脱循环。

From the Python control flow documentation: 从Python 控制流文档中:

The break statement, like in C, breaks out of the smallest enclosing for or while loop. 像C语言中一样, break语句摆脱了最小的forwhile循环。

Based on your question: 根据您的问题:

there is a iteration required #which is when Pd-the sum of P1 P2 and P3, should be less than one. #需要进行一次迭代,即当Pd-P1 P2和P3之和小于1时。

I think you want to calculate e by: 我认为您想通过以下方式计算e:

e = Pd - (P1 + P2 + P3)

It's not clear though. 尚不清楚。 Maybe you just want your if statement to be: 也许您只是希望if语句为:

if (P1 + P2 + P3) < Pd:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM