Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #multiplication table (extra credit)
- from sys import exit
- #it wasn't hard to make the table with only 1 loop.
- def try1 ():
- for i in range(9):
- x = str (i + 1)
- 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, ))
- print ()
- #this was much harder to figure out.
- def try2 ():
- n = 9
- for i in range(1, n + 1):
- for z in range(1, n + 1):
- print (i * z, end='\t')
- print ()
- def question ():
- while True:
- print ('1 or 2 or 3')
- choice = int (input ())
- if choice == 1:
- try1()
- print('Select an option:\n1. attempt 1 with a single loop.\n2. attempt 2 with two nested loops.\n3. exit program\n')
- continue
- elif choice == 2:
- try2()
- print('Select an option:\n1. attempt 1 with a single loop.\n2. attempt 2 with two nested loops.\n3. exit program\n')
- continue
- elif choice == 3:
- exit()
- def main ():
- print ('This is was very hard to do with two loops\nI did "try 1" in about 10 min and it formatted correctly\n')
- 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')
- 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')
- print ()
- print('Select an option:\n1. attempt 1 with a single loop.\n2. attempt 2 with two nested loops.\n3. exit program\n')
- question()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement