Advertisement
Limited_Ice

print multiplication table

Aug 12th, 2019
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.79 KB | None | 0 0
  1. #multiplication table (extra credit)
  2. from sys import exit
  3.  
  4. #it wasn't hard to make the table with only 1 loop.
  5. def try1 ():
  6.     for i in range(9):
  7.         x = str (i + 1)
  8.         print (str (1 * int (x)).rjust (2, ), str (2 * int (x)).rjust (2, ), str (3 * int (x)).rjust (2, ), str (4 * int (x)).rjust (2, ), str (6 * int (x)).rjust (2, ), str (7 * int (x)).rjust (2, ), str (8 * int (x)).rjust (2, ), str (9 * int (x)).rjust (2, ))
  9.     print ()
  10.  
  11. #this was much harder to figure out.
  12. def try2 ():
  13.     n = 9
  14.     for i in range(1, n + 1):
  15.         for z in range(1, n + 1):
  16.             print (i * z, end='\t')
  17.         print ()
  18.  
  19. def question ():
  20.     while True:
  21.         print ('1 or 2 or 3')
  22.         choice = int (input ())
  23.         if choice == 1:
  24.             try1()
  25.             print('Select an option:\n1. attempt 1 with a single loop.\n2. attempt 2 with two nested loops.\n3. exit program\n')
  26.             continue
  27.         elif choice == 2:
  28.             try2()
  29.             print('Select an option:\n1. attempt 1 with a single loop.\n2. attempt 2 with two nested loops.\n3. exit program\n')
  30.             continue
  31.         elif choice == 3:
  32.             exit()
  33. def main ():
  34.     print ('This is was very hard to do with two loops\nI did "try 1" in about 10 min and it formatted correctly\n')
  35.     print ('I did most of "try 2" as quickly as the first and knew that I had to modify the "print" output to make it work')
  36.     print ('I knew that I had to modify the output of print similar to backslash "n" so I looked up different modifiers and tried them\n adding a tab space at the end seemed to the trick\n although it is offset to the wrong side')
  37.     print ()
  38.     print('Select an option:\n1. attempt 1 with a single loop.\n2. attempt 2 with two nested loops.\n3. exit program\n')
  39.     question()
  40.  
  41. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement