简体   繁体   中英

How to continue looping the if statement once satisfied and no longer looping the whole if and else

Once ti == t_open is satisfied (which is the if statement) how do i continue making it do the if statement and no longer the else statement even though the ti == t_open is no longer satisfied? I tried using continue but it does not work.

import math
def trackflow(f_in, f_out, r, H, h, t_max, t_open):
    t = 0.1
    vi = 0
    hi = 0
    ti = 0

    while ti < t_max and hi < H and hi >= 0:
        if ti == t_open:
            v_2 = vi + (f_in - f_out)*t
            h_2 = hi + ((f_in - f_out)*t)/(math.pi*r**2)
            t_2 = ti + 0.1

            print(round(v_2,1))
            print(round(h_2,2))
            print(round(t_2,1))

            continue

        else:
            f_out = 0 
            v_2 = vi + (f_in - f_out)*t
            h_2 = hi + ((f_in - f_out)*t)/(math.pi*r**2)
            t_2 = ti + 0.1

            print(round(v_2,1))
            print(round(h_2,2))
            print(round(t_2,1))

        vi = v_2
        hi = h_2
        ti = t_2

I don't really know what you're trying to do but I guess you can do something like this

import math
def trackflow(f_in, f_out, r, H, h, t_max, t_open):
   t = 0.1
   vi = 0
   hi = 0
   ti = 0

   def tiEqTOpen():
       v_2 = vi + (f_in - f_out)*t
       h_2 = hi + ((f_in - f_out)*t)/(math.pi*r**2)
       t_2 = ti + 0.1

       print(round(v_2,1))
       print(round(h_2,2))
       print(round(t_2,1))

   def tiNEqTOpen():
       f_out = 0 
       v_2 = vi + (f_in - f_out)*t
       h_2 = hi + ((f_in - f_out)*t)/(math.pi*r**2)
       t_2 = ti + 0.1

       print(round(v_2,1))
       print(round(h_2,2))
       print(round(t_2,1))

   while ti < t_max and hi < H and hi >= 0:
       if ti == t_open:
           tiEqTOpen()

           continue

       else:
           tiNEqTOpen()

       vi = v_2
       hi = h_2
       ti = t_2
   else:
       while <the_new_condition>:
           <your_logic_using_the_inner_functions_above>

I hope it helps.

does this mean the same as what i wrote above why does it come up with an error 
v_2 is not defined? 



import math
def trackflow(f_in, f_out, r, H, h, t_max, t_open):
t = 0.1
vi = 0
hi = 0
ti = 0

def tiEqTOpen():
   v_2 = vi + (f_in - f_out)*t
   h_2 = hi + ((f_in - f_out)*t)/(math.pi*r**2)
   t_2 = ti + 0.1

   print(round(v_2,1))
   print(round(h_2,2))
   print(round(t_2,1))



def tiNEqTOpen():
   f_out = 0 
   v_2 = vi + (f_in - f_out)*t
   h_2 = hi + ((f_in - f_out)*t)/(math.pi*r**2)
   t_2 = ti + 0.1

   print(round(v_2,1))
   print(round(h_2,2))
   print(round(t_2,1))


while ti < t_max and hi < H and hi >= 0:
   if ti == t_open:
       tiEqTOpen()


   else:
       tiNEqTOpen()

   vi = v_2
   hi = h_2
   ti = t_2
is this what you mean i should do?


import math
def trackflow(f_in, f_out, r, H, h, t_max, t_open):
t = 0.1
vi = 0
hi = 0
ti = 0

ti_topen_was_true = False

while ti < t_max and hi < H and hi >= 0:
    if ti == t_open:
        v_2 = vi + (f_in - f_out)*t
        h_2 = hi + ((f_in - f_out)*t)/(math.pi*r**2)
        t_2 = ti + 0.1

        print(round(v_2,1))
        print(round(h_2,2))
        print(round(t_2,1))

        or ti_topen_was_true

        True



    else:
        f_out = 0 
        v_2 = vi + (f_in - f_out)*t
        h_2 = hi + ((f_in - f_out)*t)/(math.pi*r**2)
        t_2 = ti + 0.1

        print(round(v_2,1))
        print(round(h_2,2))
        print(round(t_2,1))

    vi = v_2
    hi = h_2
    ti = t_2

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