-1

I want a while loop with increment of 10 without repeating the starting number in python. This is what i have done, and the result i am getting, I have written the required result below for further clarifications. Thank you.

limitstart = 0
limitend = 0
while limitstart < 55:
    limitstart = limitend
    limitend = limitstart + 10
    print (limitstart)
    print (limitend)
    print ("new batch")

This is my result.

0
10
new batch
10
20
new batch
20
30
new batch
30
40
new batch
40
50
new batch
50
60
new batch
new batch
60
70
new batch

The result i want is :

1
10
new batch
11
20
new batch
21
30
new batch
31
40
new batch
41
50
new batch
51
55
3
  • 1
    Sounds like you actually want a for loop: for number in range(0, 71, 10): Commented May 23, 2020 at 12:53
  • 1
    can you kindly explain? Commented May 23, 2020 at 13:02
  • Does this answer your question? python for increment inner loop Commented May 23, 2020 at 13:55

2 Answers 2

0

Even though I don't understand quite why you want to do this using a while loop, you can change your code to:

limitstart = 0
limitend = 0
limit = 55
while limitend < limit:
    limitstart = limitend
    if limitstart + 10 > limit:
        limitend = limitstart + (limit-limitend)
    else:
        limitend = limitstart + 10
    limitstart += 1
    print (limitstart)
    print (limitend)
    print ("new batch")

Output:

1
10
new batch
11
20
new batch
21
30
new batch
31
40
new batch
41
50
new batch
51
55
new batch
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. But could i have done it any other way? Probably a for loop? I am open to more ideas and answers please
0

Also as a for loop:

limit=55
for i in range(1,limit,10):
    print(i)
    print(i+9 if i+9 <= limit else i+(limit-i))
    print("new batch")

output:

1
10
new batch
11
20
new batch
21
30
new batch
31
40
new batch
41
50
new batch
51
55
new batch

Comments

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.