1

Just started to learn programming.. here my question, using Python how I can write a while loop inside another while loop??

while A > 10:
B +=10

    while x < 12:
       x +=1
       A =  bla bla some function depending on B

how is the correct syntax??

Thanks a Lot!

6
  • 2
    I'm not really sure what the question is. Did you try something and it didn't work? If that's the case you should show us what didn't work. Because unless you don't update A properly such that you get termination, this is fine. The syntax is certainly correct (obv except for the fact that "bla bla some function...." is not python) Commented Oct 28, 2013 at 19:16
  • 1
    @Cruncher: There's also the fact that B+=10 needs to be indented. Commented Oct 28, 2013 at 19:16
  • @jwodder oops, yes. That. I was assuming that the first loop was correct given the question, so I didn't really validate that part xD Commented Oct 28, 2013 at 19:17
  • stackoverflow.com/questions/1421323/nested-while-loop-in-python Commented Oct 28, 2013 at 19:24
  • what do u mean with "needs to be indented"?? Commented Oct 28, 2013 at 19:51

1 Answer 1

5

This is a nested while inside another one. Remember to indent correctly.

while A > 10:
    B += 10
    while x < 12:
        x += 1
        A = bla bla some function depending on B
Sign up to request clarification or add additional context in comments.

2 Comments

Edited so it matchs Official Python style.
Maybe you'll have to declare A before entering the while loop (e.g. by setting it to True.

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.