简体   繁体   English

具有不同和相关步骤的嵌套循环

[英]Nested loops with different and dependant steps

I need to make an algorithm to perform a task with 2 while loops.我需要制定一个算法来执行具有 2 个 while 循环的任务。 I will explain what it is I want to achieve.我将解释我想要实现的目标。 I need to make a loop based on length of h value.我需要根据 h 值的长度进行循环。 Well the loop should take big steps with f.ex.那么循环应该与 f.ex 一起迈出一大步。 3 or 2 or 4, the item from while or for loop should check a condition or equation. 3 或 2 或 4,while 或 for 循环中的项目应检查条件或方程式。 I need to check when or which value of item gives zero value to n in equation.我需要检查何时或哪个项目的值给方程中的 n 赋予零值。 When n value reaches zero, I need to perform a smaller step with 0.1.当 n 值达到零时,我需要使用 0.1 执行更小的步骤。 Because I need to be more precis.因为我需要更精确。 There is no need to start the loop with small step in the beginning it slows performance time, and it is just interesting to know exact small value of item which gives zero to n equation.没有必要在开始时以小步长开始循环,它会减慢执行时间,并且知道 item 的精确小值会给出零到 n 方程的值是很有趣的。 And this is true when n equation closes to zero value.当 n 方程接近零值时,这是正确的。

To illustrate the example: Loop 1 starts and take big steps.举例说明:循环 1 开始并迈出大步。

h = 50
st = 10
loop 1 starts
0 ,       3,        6,      9,       12,     15,     18,  21,  24 …………………                            #<--------   steps from h value 
25.0 , 22.0 , 19.0,  16.0, 13.0, 10.0, 7.0, 4.0, 1.0 …………………                                         #<--------  calculate values from n equation
                                                  If n <= 2 or n>= -2:
                                                  |_____ loop 2 starts with: 
                                                          24.0, 23.9 , 23.8, 23.7, 23.6, ……………..     #<--------   small steps iterate with hj = 24+st
                                                          0.4,  0.3 , 0.1, 0.0, -1.0, ……………..        #<--------  calculate values from n equation
                                                          Calculate n values from n equation
                                                          M = 2 + n
                                                          If (n <= 0 or n>= -1) and M==2 :           #<--------   If statement is fullfilled then return some values and quit loop 2 and loop 1.
                                                             Return n, itemstep from loop 2, M
                                                           
                                                          Elif (n <= 0 or n>= -1) and M!=2:
                                                             Quit loop 2 and 1                       #<--------   If statement is not fullfilled then quit loop 2 and loop 1.

The reason for n >= -1 , because we could start step from 50 down to 0 and n value can be negative as well. n >= -1的原因,因为我们可以从 50 开始逐步下降到 0,并且 n 值也可以是负数。 I have tried to figure it out with code below, but it seems not working, is it a way to generate such code in while or for loop.我试图用下面的代码来解决它,但它似乎不起作用,这是在 while 或 for 循环中生成此类代码的一种方法。

M = 2         #<---------- to check conditions
j = 0 
st = 10 
h = 50        #<---------- to iterate over length h
n = h/2 - j   #<---------- to calculate n values (n equation)
result =[]    
while j <=h:    #<----------  loop 1
    n = h/2 - j
    if n <= 3 or n >= -3:  #<---------- if this is satisified then start loop2 
        print(n)
        i=j
        while i < 10+st:       #<----------  loop 2
            n = h/2-i         
            M = 2+n
            if (n <= 0 or n>= -1) and M==2:
                result.append(n, i, M)       #<----------  return values
                i = 70+j                     #<---------- stop loop 2 and loop 1
                j = h+70
            elif  (n <= 0 or n>= -1) and M!=2:  #<---------- if this is not satisified then quit loop2 and loop 1
                i = 70+j                     #<---------- stop loop 2 and loop 1
                j = h+70
            i +=0.1
            j += i
    j += 3

This might help you some.这可能会对你有所帮助。 (Not exactly what you wanted but it is a start) (不完全是你想要的,但这是一个开始)

j = 0
h = 50
while j <= h:
    n = h/2 - j
    if -3 <= n <= 3:
        i = j
        while i < j + 10:
            n = h/2 - i
            if n <= 0:
                j = h + 70
                print(n)
                break
            i += 0.1
    j += 3

or you could do it like this.或者你可以这样做。

j = 0
h = 50
while j <= h:
  n = h/2 - j
  if -3 <= n <= 3:
    break
  j += 3

i = j
while i < j + 10:
  n = h/2 - i
  if n <= 0:
      print(n)
      break
  i += 0.1

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

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