0

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

**

1
  • 2
    Couldn't you do something like x = 10*(i // 10 + 1)? Commented Sep 26, 2022 at 1:15

1 Answer 1

1

Modulo is a nice way of doing that:

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

Edit: you should probably initialize x outside the loop.

Sign up to request clarification or add additional context in comments.

1 Comment

I think you mean if i % 10 == 0. The code given will increment x 9 times out of 10...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.