#!/usr/bin/python3 # Print a multiplication table, an in-class example for CS 151 # # Authors: Jeff Kinne, jkinne@cs.indstate.edu # Change-log: # 23-Aug-2019 - initial code, print 10x10 # 30-Sep-2019 - separated code into functions # - made generic so can run different sized tables # - put in comments like I would like students to use for projects # # Starting point: code was from scratch, no other starting point # Sources: none # Assistance from: nobody # # To use: Set the value of n below the function definitions # and then run with - python3 multiply.py or ./multiply.py # Note - this file contains comments in the style that the 9am section is # supposed to use. See cs.indstate.edu/cs151/study.html for details. # Print a head line for a multiplication table # n - length/width of the table (goes 0 to n) def headerLine(n): print(' \t', end='') # print from 0 to n with tabs in between for y in range(0, n+1): print(y, '\t', end='') print('') # print a row of ------ print('-'*(8*(n+2))) # Print a line from a multiplication table # n - length/width of the table (goes 0 to n) # x - which row to print (will print x*0, x*1, x*2, ...) def multiplyLine(n, x): # print x| as the label for the row print(x, '|\t', end='') # print x*0, x*1, ..., x*n for y in range(0, n+1): print(x*y, '\t', end='') print('') # pick a value of n, we could also do n = int(input()) n = 5 # print a header line and then all the rows of the table headerLine(n) for x in range(0, n+1): multiplyLine(n, x)