0
condition = True
for index in range(35, 100):
    if condition == True:
        index += 5
    print(index)

The for-loops gives me the result of 40 41 ...... 101 102 103 104 105

I hope that index can be like 35 40 45 50 55.......100

but it's seems like doesn't work properly.

3 Answers3

0
for index in range(35, 100)

This is similar to : for (int index = 35; index < 100; i++) So after each itertion you will have index += 1

To solve your problem, you will have to add steps to your range: range(35, 100, 5)

for index in range(35, 100, 5):
    print(index)

-- EDITED

or you can also (as mentionned in the comment):

condition = True
for index in range(35, 100):
    if condition == True:
        index += 4
    print(index)
0

use a while loop instead

condition = True
index = 35
while index < 100:
    if condition == True:
        index += 5
    else:
        index += 1
    print(index)

This way depending on the condition you will add 5 or 1. We need a while loop because we don't exactly know how many time we will go through the loop, so a for isn't what you need.
According to this post you can't add numbers to the iterator in a for i in range() loop

Lwi
  • 354
  • 4
  • 10
0

I think you are almost there, you just need to specify the condition in the loop like this. Your "for loop" will also produce the same result.

condition = True
for index in range(35, 100, 5):
    if condition == True: 
        index += 5
    print(index)