0

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

3
  • 3
    There are a gazillion way to get your desired output, but I haven't understood what you are trying to do Commented Aug 13, 2022 at 11:11
  • There are a gazillion way to skin the cat, I wonder which answer is the most efficient using timeit Commented Aug 13, 2022 at 12:16
  • i want to count from 0 to 1000 but jump 2 steps at every count of 5. e.g. 2,3,4,5,6, 8,9,10,11,12, 14,15,16,17,18...... Commented Aug 13, 2022 at 12:23

4 Answers 4

2

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)
Sign up to request clarification or add additional context in comments.

1 Comment

You can use range in a for-loop to remove defining and stepping i yourself: for i in range(2,1000,6)
1

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>

Comments

0

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

1 Comment

i want to count from (i,j,k) 0 to 1000 but jump 2 steps at every count of 5. e.g. 2,3,4,5,6, 8,9,10,11,12, 14,15,16,17,18......
0

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)

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.