简体   繁体   中英

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. 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.

The value of e is -229.22788463 on the first iteration, so you're breaking out of the loop.

From the Python control flow documentation:

The break statement, like in C, breaks out of the smallest enclosing for or while loop.

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.

I think you want to calculate e by:

e = Pd - (P1 + P2 + P3)

It's not clear though. Maybe you just want your if statement to be:

if (P1 + P2 + P3) < Pd:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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