简体   繁体   中英

Change value every nth iteration python

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

**

Modulo is a nice way of doing that:

if i % 10 == 0:
  x+=10

Edit: you should probably initialize x outside the loop.

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