I am just starting out learning code.

The output of the following code is OOOXOXOO, but I don't see how X can be printed twice. How does a=2 more than once?

a = 1
while a < 3:
    b = 1
    while b < 3:
        if a == 2:
            print ("X"),
        print ("O"),
        b = b + 1
    print ("O"),
    a = a + 1

closed as off-topic by dkaeae, Evil, xskxzr, Discrete lizard Jul 3 at 12:10

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not appear to be about computer science, within the scope defined in the help center." – dkaeae, Evil, xskxzr, Discrete lizard
If this question can be reworded to fit the rules in the help center, please edit the question.


Think carefully about the flow.

Your innermost While loop runs through b = 1, 2, each time it hits.

It does this for EACH value of a in the outermost loop.

So when a == 2, we progress inward and run through that loop twice. a == 2 both times.

  • $\begingroup$ Okay that makes sense, thank you so much. I'm running through some course material on my own, so I don't have a professor to ask about this. I was confused about which order the loops would run in. Thanks again for taking the time to help me with this! $\endgroup$ – BenG Jul 1 at 15:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.