Home > Blockchain >  Add count until certain value then reset to zero python
Add count until certain value then reset to zero python

Time:09-27

I am trying to change the value of x depending on the length of my list

How can x count up by 5 through each iteration and then back to 0 after the 10th round?

list = {"one", "two".....fifty} #example shortened 
listLen = len(list) # 50

for i in range (0, listLen) # 0 - 49

    x = ??? #  5 max 45 
    
    ops.update(location=x)

Desired outcome:

0. x = 0
1. x = 5
2. x = 10
...
9. x = 45

10. x = 0
11. x = 5
12. x = 10
...
19. x = 45
...



(0,5,10,15,20,25,30,25,40,45,0,5,10,15,20,25,30,25,40,45            
 0,5,10,15,20,25,30,25,40,45,0,5,10,15,20,25,30,25,40,45
 0,5,10,15,20,25,30,25,40,45) 

CodePudding user response:

Try this

outcome = []
lst = {1,2,3,...,50}
lstLen = range(0, len(lst), 5)

for a in range(len(lst)):
  outcome.append(lstLen[a])

print(outcome)

Output

[0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 0, 5, 10, 15, 20, 25, 30, 35, 40, 45,
 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 
0, 5, 10, 15, 20, 25, 30, 35, 40, 45]

CodePudding user response:

Try this:

ans = []
for i in range (0, listLen): # 0 - 49
    x  = 5
    ans.append(x)
    if i % 9 == 0:
        x = 0
  • Related