""" Purpose: stepping stone to nested looping """ # nested looping: loops inside of loops # why do we use them? allows us to access individual cells # get number of rows and columns reply = input( "Number of rows and columns: " ) srows, scols = reply.split() nrows = int( srows ) # can't use int() on multiple things at once! ncols = int( scols ) print() # since we haven't messed with end= yet, this will print a blank line # since the output cursor is already on a newline and move output to a newline # 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 r in range( 0, nrows ) : # since we want r and c to be numbers, we loop through range() s print("row", r, ":", end=" ") # range( 0, n ) will go from 0 to n-1 # i added an end= to this print() statement because pretty output makes me happy :) total = 0 for c in range( 0, ncols ) : # determine cell value for column c cell = r + c # r and c are integers, so when we add them together we get another integer # the first run of the outer for loop, r = 0 for each row so cell = c # the second run r = 1 so each cell = c + 1 # third r = 2 cell = c + 2 # total = total + cell # if we wanted to report the total for the row, we would do it in the # empty print() outside of this nested loop but still in the outer row loop # print cell value print( cell, end="\t" ) # separates values with a tab ( = 5 spaces ) # how do we start a new row after printing each column? print() # sends subsequent output to a newline # this print() is at this level of indentation because we want to go to a new line # once we've processed the entire row, not after every cell # print() says print nothing and move output to a newline because # there is an invisible end="\n" in every print() statement # end= in a print statement dictates where the next output will go # empty print() will print nothing and move the cursor for output to a newline # this will be incredibly relevant to a homework assignment # remember all of my in-class notes are available under the meetings tab on the website