Advertisement
Guest User

multiplication_table_w_input

a guest
Jul 25th, 2014
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.85 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.  
  11.     if not rows:
  12.         rows = cols
  13.    
  14.     # Calculate column width (minimum 2 chars spacing)
  15.     width = len(str(rows * cols)) + 2
  16.  
  17.     # First row: header
  18.     for num in range(1, cols+1):
  19.         print '\t', num, '  ',
  20.     print '\n' + '-' * ((cols) * 8 )
  21.    
  22.     # Rest of rows
  23.     for row in range(1, rows+1):
  24.         print row, ' | \t',
  25.         for col in range(1, cols+1):
  26.             print row*col, '\t',
  27.         print
  28.  
  29.  
  30. def main():
  31.     end = input('Up to what number: ')
  32.     endnum = int(end)
  33.     if endnum > 14:
  34.         print 'That number is too high!'
  35.     else:
  36.         show_mtable(endnum)
  37.  
  38.  
  39. if __name__ == "__main__": main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement