''' Purpose: introduce nested looping. for a user-supplied n, loop n times where on the i-th iteration, the integers from 0 to i are printed out. ''' import pause # get integer of interest reply = input( 'Enter number: ' ) n = int( reply ) # repeat n times # output = '' # if you initialize output here, you get a different output because of the way the loops are structured...play with it and see what happens for row in range(0, n) : # 3 things happen in this for loop: output gets initialized, the next for loop is initiated, and then output is printed # we want to output the numbers 0 through i # first output is 0, next one is 0 1, then 0 1 2, ..., and finally output 0 1 2 ... n-1 # print(i) # not what we want...just outputs i output = '' # this is an accumulator for the nested for loop below for column in range(0, row+1): # 0 to i INCLUDING i # print(j) # this gets the output we want, but not on the same line... # output = output + j # by itself results in error TypeError: must be str, not int # convert j to a string js = str(column) output = output + js + " " # adds a space between the numbers print(output) # play with the indentation of this line and see what happens...INDENTATION MATTERS # pause.enter_when_ready() # n is the number of times we want work to be done; for this specific algorithm, it's the number of rows outputted # we could probably do this code using a list and appending to it, # but if we want just a string output, using a list requires a little bit more hassle print() print("All done!")