7r0jan005
7r0jan005

Reputation: 31

how to loop step and count in python

I want to be able to count 5 at every step of 2 while condition is less than 1000 for example:

i = 0
j = 2000
k = 3000
while i < 1000:
    i += 2
    for x in range(5):
        print(i)
    j += 2
    for x in range(5):
        print(j)
    k += 2
    for x in range(5):
        print(k)

but the output just print i, j, k 5 times

output:::
2
2
2
2
2
2002
2002
2002
2002
2002
3002
3002
3002
3002
3002
4
4
4

I want the the result to be: .....

2
3
4
5
6
2002
2003
2004
2005
2006
3002
3003
3004
3005
3006
8      #please note here that 8(i) continue by 2 steps from 6
9
10
etc..........

i will like to know a more simpler and pythonic way to do this. Thanks

Upvotes: 0

Views: 133

Answers (4)

Sidharth Mudgil
Sidharth Mudgil

Reputation: 1461

You can do this in other ways but here is my approach

j = 2000
k = 3000
for i in range(2, 1000, 6):
    for x in range(5):
        print(i+x)
    j += 2
    for x in range(5):
        print(j+x)
    k += 2
    for x in range(5):
        print(k+x)

Upvotes: 0

threadfin
threadfin

Reputation: 163

So you're trying to get number progression, here's one way to do it:

progression = [a for a in range(2,7)] \
            + [b for b in range(2002,2007)] \
            + [c for c in range(3002,3007)]
for i in range(0, 1000, 6):
    for p in progression:
        print(p + i)

2
3
4
5
6
2002
2003
2004
2005
2006
3002
3003
3004
3005
3006
8
9
10
11
12
2008
2009
2010
2011
2012
3008
3009
3010
3011
3012
14
15
16
17
18
2014
2015
2016
2017
2018
3014
3015
3016
3017
3018
20
21
22
23
24
2020
2021
2022
2023
2024
3020
3021
3022
3023
3024
26
...<truncated>

Upvotes: 1

Ciro Garc&#237;a
Ciro Garc&#237;a

Reputation: 650

That's a weird thing you're trying to do, but here is my modified version with your desired output:

a, b, c = 0, 2000, 3000
for i in range(2, 1000, 6):
    for x in range(5):
        print(a+i+x)
    for x in range(5):
        print(b+i+x)
    for x in range(5):
        print(c+i+x)

Upvotes: 2

WhoKnowsMe
WhoKnowsMe

Reputation: 593

I don't really undestand what you want to do but this output what you want:

i = 0
j = 2000
k = 3000
while i < 1000:
    i += 2
    for x in range(5):
        print(i+x)
    j += 2
    for x in range(5):
        print(j+x)
    k += 2
    for x in range(5):
        print(k+x)
    i += 4

Upvotes: 0

Related Questions