Home > Blockchain >  Change value every nth iteration python
Change value every nth iteration python

Time:09-27

How can I change a value inside a loop every 10th iteration?

Say I have a list with 30 properties

myList = {"One", "Two", "Three", "Four"........,"Thirty" } #trimmed
listLength = len(myList) #30
listRange = range(0, listLength) # 0-30

for i in listRange
    
    x = ???
    ...
    
    ops.mesh.add(size=2, location=(x)) # change value of x by 10 every 10 value
    

I would like to change the value of x by a factor of 10 every 10th time.

Desired outcome

first 10 values x = 10
second 10 values x = 20
third 10 values x = 30

**

CodePudding user response:

Modulo is a nice way of doing that:

if i % 10 == 0:
  x =10

Edit: you should probably initialize x outside the loop.

  • Related