1

Here is a for loop in C++. How would you convert this to Python?

for(j=1, k=x; j<=x; j++, k--)

3 Answers 3

3
j = 1
k = x
while j <= x:
   j = j + 1
   k = k - 1
Sign up to request clarification or add additional context in comments.

1 Comment

If this solved your problem, please select this as the correct answer and mark this solved.
1
j = 1
k = x
while j <= x:
   j += 1
   k -= 1

same as previous small changed

Comments

0

I just think so:

x = 5
k = x
for j in range(1, x+1, 1):
    # do something
    print(j, k)
    
    k -= 1

And it output:

(1, 5)
(2, 4)
(3, 3)
(4, 2)
(5, 1)

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.