''' Purpose: stepping stone to nested looping ''' # get number of rows and columns reply = input( 'Number of rows and columns: ' ) srows, scols = reply.split() # we need to convert into ints here because input() returns string! nrows = int( srows ) ncols = int( scols ) # nested for loops! PLZ REVIEW THIS # you can have a for loop in another for loop # produce nrows rows of output # for each row produce ncols columns of output # row r's c-th output, is value of r + c # for each row in the range 0 (inclusive) to nrows (exclusive) # r includes 0 and goes up to but not including nrows --> [0, 1, 2, ... nrows-1] for r in range( 0, nrows ): print( 'row', r, ':' ) # PLZ MAKE SURE YOU UNDERSTAND THIS - ask if you're confused :) # for each row, we process EACH COLUMN # this inner for loop will run to COMPLETION before the outer for loop starts on ITS next iteration # then this inner for loop will run to completion again for the next outer loop # and so on until the outer for loop finishes!!! # this for loop occurs for each of the for r loop! runs to completion! for c in range( 0, ncols ): # print( 'r =', r, 'c =', c ) # print( ' ', c, end=' ' ) # if we add end=, then we print each column number on the same line # for each row # determine cell value for column c cell = r + c # print cell value print(cell, end="\t") # if nrows = 2, ncols = 3 # r = 0 # c = 0 # cell = 0 + 0 = 0 # c = 1 # cell = 0 + 1 = 1 # c = 2 # cell = 0 + 2 = 3 # r = 1 -- outer loop starts its next iteration (the next value) # c = 0 -- inner loop starts over again # cell = 1 + 0 = 1 # c = 1 # cell = 1 + 1 = 2 # c = 2 # cell = 1 + 2 = 3 # since r = 2, the outer for loop finishes and the code exits # reaches this point when the inner for loop finishes BUT STILL IN THE OUTER FOR LOOP print() # this will end the current line then so the next row will print on a new line # to comment several lines at the same time --> highlight them --> cntrl/command + forward slash key