Home > other >  Counting the number of iteration in a loop
Counting the number of iteration in a loop

Time:12-07

I cannot get the correct number of the iterations for each steps. Is there any way to solve this problem?

The output for this code is [(5, 1), (8, 2)], which should be [(5, 100), (8, 63)]

CodePudding user response:

you can do it this way:

step=[5,8]
lst= []
counter = 0
for x in step:
    for i in range(0, 500, x):
        counter  = 1
    lst.append(counter)
print(lst)
output=list(zip(step,lst))
print(output)

CodePudding user response:

when you use len(step) as your increment in your for loop, python is using 2 as the step increment, since the step list contains 2 items. What you want is to run your loop once for each step interval present in your step list.

step=[5,8]
lst= []

for s in step:
    counter = 0
    for i in range(0, 500, s):
        counter  = 1
    lst.append(counter)

output=list(zip(step,lst))
print(output)
  • Related