Advertisement
Guest User

multiplication_table

a guest
Jul 25th, 2014
739
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.72 KB | None | 0 0
  1. #!/usr/bin/python2.7
  2. # Multiplication table. http://redd.it/2agwnq
  3. # by tacogratis, adapted from  wukaem
  4.  
  5. def show_mtable(cols, rows=None):
  6.     """
  7.   Function printing multiplication table.
  8.   rows, cols - ints, dimensions of table. If rows not provided: rows = cols.
  9.   """
  10.     if not rows:
  11.         rows = cols
  12.    
  13.     # Calculate column width (minimum 2 chars spacing)
  14.     width = len(str(rows * cols)) + 2
  15.  
  16.     # First row: header
  17.     for num in range(1, cols+1):
  18.         print '\t', num, '  ',
  19.     print '\n' + '-' * ((cols) * 8 )
  20.    
  21.     # Rest of rows
  22.     for row in range(1, rows+1):
  23.         print row, ' | \t',
  24.         for col in range(1, cols+1):
  25.             print row*col, '\t',
  26.         print
  27.  
  28.  
  29. def main():
  30.     show_mtable(10)
  31.  
  32.  
  33. if __name__ == "__main__": main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement