443,402 Members | 1,047 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,402 IT Pros & Developers. It's quick & easy.

# How to add " | " to a mutliplication table?

 P: 32 I am having trouble trying to add the "|" character to my multiplication table. The output is suppose to have a column on the far left reading 1,2,3,4, etc. and have the "|" character separating it from the rest of the table. This is what I have at the moment. Expand|Select|Wrap|Line Numbers ''' This program allows a user to make a multiplication table of their choise in size. ''' number = int(raw_input('What size multiplication table would you like: '))   i = 1 print "-" * 50 while i < 11:     n = 1     while n <= 10:         print "%4d" % (i * n),         n += 1     print ""     i += 1 print "-" * 50   raw_input('Press ENTER to continue...')   Nov 5 '10 #1

#### ✓ answered by bvdet

I think this will do what you want:
Expand|Select|Wrap|Line Numbers
1. number = int(raw_input('What size multiplication table would you like: '))
2.
3. i = 1
4. print "-" * 100
5. while i < 11:
6.     print "|".join(["%4d" % (i), "".join(["%4d" % (i*n) for n in range(2,11)])])
7.     i += 1
8. print "-" * 100
9.
10. raw_input('Press ENTER to continue...')
Issuing multiple print statements is slow compared to joining strings and issuing one print statement. In the above example, I am joining two strings with the "|" character, the current value of "i" and another string. The other string is created by joining a list of strings created with a list comprehension. The list comprehension is equivalent to:
Expand|Select|Wrap|Line Numbers
1. strlist = []
2. for n in range(2,11):
3.     strlist.append("%4d" % (i*n))
HTH

4 Replies

 Expert Mod 2.5K+ P: 2,851 I think this will do what you want: Expand|Select|Wrap|Line Numbers number = int(raw_input('What size multiplication table would you like: '))   i = 1 print "-" * 100 while i < 11:     print "|".join(["%4d" % (i), "".join(["%4d" % (i*n) for n in range(2,11)])])     i += 1 print "-" * 100   raw_input('Press ENTER to continue...') Issuing multiple print statements is slow compared to joining strings and issuing one print statement. In the above example, I am joining two strings with the "|" character, the current value of "i" and another string. The other string is created by joining a list of strings created with a list comprehension. The list comprehension is equivalent to: Expand|Select|Wrap|Line Numbers strlist = [] for n in range(2,11):     strlist.append("%4d" % (i*n)) HTH Nov 5 '10 #2

 P: 32 I need the output to show like a real multiplication table would. Example: http://www.dingeman.org/images/times-table-12x12.gif Nov 5 '10 #3

 Expert Mod 2.5K+ P: 2,851 The same principles will apply. Nov 5 '10 #4

 P: 32 How would I add "1 through x" above the multiplication table to make it look like an actually multiplication table to this code? I was able to get the "|" character in the table, but can't figure out the other part. Expand|Select|Wrap|Line Numbers x = int(raw_input('What size multiplication table would you like: ')) for row in range(1, x + 1):     print "|".join(["%4d" % (row), "".join(["%4d" % (row*col) for col in range(1, x + 1)])])     The correct output is suppose to look something like this: Expand|Select|Wrap|Line Numbers  |   1   2   3   4   5 ---------------------- 1|   1   2   3   4   5 2|   2   4   6   8  10 3|   3   6   9  12  15 4|   4   8  12  16  20 5|   5  10  15  20  25   Nov 5 '10 #5