Convert a For loop to While loop in python

后端 未结 1 1693
庸人自扰
庸人自扰 2021-01-29 16:11

I am trying to convert these two for loops to while loops:

sum = 0
for i in range (10, 30):
    for j in range(i, 10*i):
               


        
相关标签:
1条回答
  • 2021-01-29 16:36

    What for i in range(a,b) does is it runs the loop for value of i starting from a till it reaches b-1 , same you can replecate with while loop statement. What we are doing here is before starting the loop we initiated i to be qual to a and then keep on increasing the value by order of 1 after each iteration.

    And before starting next iteration if it's less that b if not we don't start the next iteration.

    Watch this tutorial for more info.

    sum = 0
    i = 10
    while i <30:
        j=i
        while j < (10*i):
            sum += j
            j+=1
        i+=1      
    
    0 讨论(0)
提交回复
热议问题