Does a nested loop have to use the variable from the outer loop?

Question

This exercise shows the nested loop using the variable from the outer loop. Does the nested loop always need to use the variable from the outer loop?

Answer

No, the nested loop doesn’t HAVE to use the variable from the outer loop. Although this is the most common application for a nested loop, it’s completely fine to have the nested loop perform some activity that does not involve the variable from the outer loop.

The following example shows two loops which both iterate over range() lists to produce a grid pattern. The nested loop operates independently of the outer loop variable.

for x in range(5):
    for y in range(5):
        print("X ",end='')
    print()
9 Likes

Could you please explain the grid example more? Why does this produce a grid pattern?

(I understand nested loops. I don’t understand this particular example.)

Thanks.

1 Like

Well this iterator checks every value of X with each value of Y and prints an X. See X and Y as axes like so:

0   1   2   3   4   5      X-axis
1
2
3
4
5
Y-axis

If you then iterate along the values of X and Y you create a pattern like so:

0   1   2   3   4   5      X-axis
1.  X   X   X   X   X
2.  X   X   X   X   X
4.  X   X   X   X   X
5.  X   X   X   X   X
Y-axis
5 Likes

The magic is in the print statement which directs the console to keep the pencil on the same line during iteration of each row (each value of x). The print statement that follows that directs the pencil to start on the next line when x changes.

We should note that x and y have reversed roles in @janneslohmeijer’s example. Technically your code would be,

for y in range(5):
    for x in range(5):
        print ("X ", end='')
    print ()

such that the y-axis represents the row number, and the x-axis represents the column number. A minor point, but worth consideration.

15 Likes
for y in range(5):
    for x in range(5):
        print ("X ", end='')
    print ()

I got syntax error at end=’ ’

That is because Python 2 doesn’t recognize that syntax which was introduced in Python 3.

>>> for y in range(5):
    for x in range(5):
        print "X ",
    print

    
X  X  X  X  X 
X  X  X  X  X 
X  X  X  X  X 
X  X  X  X  X 
X  X  X  X  X 
>>> 
3 Likes

Thank you so much.:slightly_smiling_face:

1 Like

Hi, can you explain the purpose of end=’ ’ in the loop?

for y in range(5):
for x in range(5):
print ("X ", end=’’)
print ()

1 Like

That is not part of a loop, it is an argument to the print function.
You can run
help(print)
or search the web for print’s documentation, to find out what print does with that argument.

2 Likes

ohh, what is outer loop, haven’t mention anywhere before yet. when explain please be specific.

1 Like
a = [11, 23, 34, 45, 56]
b = [5, 6, 7, 8, 9]

for m in a:            # outer loop
    for n in b:        # inner loop
        # code block
3 Likes

Why is the X capitalized?

print "X" 

Because it is a bigger footprint on the screen. No other reason than that.

Not sure if this is question is in the right section but here it goes. Why can’t you write element += scoops_sold instead of scoops_sold += element?

Thanks for any help.

Which quantity is accumulating?

Compound assignments are not commutative.

a += b

is not the same as,

b += a

the way,

a + b

is the same as,

b + a

Commutative property of addition

Well actually + isn’t either.

"hello " + "world"
"world" + "hello "

@data9180135853
increasing a by b is different from increasing b by a

2 Likes

I didn’t know you could do the help(print) and get instructions on different features within the print function that can be utilized… end=’’ helped me solve a hackerrank puzzle that had me scratching my head… thanks mate <3

hello there! A outer loop is something that does not have 2 tabs or a tab.

Why do we need to write the print() at the end of the outer loop

The print statements in the inner loop have no newline characters so everything prints on one line. The last print inside of the outer loop is to reset the pencil to a new line.

1 Like