By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,402 Members | 1,047 Online
Bytes IT Community
+ 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
  1. '''
  2. This program allows a user to make a multiplication table of their choise in size.
  3. '''
  4. number = int(raw_input('What size multiplication table would you like: '))
  5.  
  6. i = 1
  7. print "-" * 50
  8. while i < 11:
  9.     n = 1
  10.     while n <= 10:
  11.         print "%4d" % (i * n),
  12.         n += 1
  13.     print ""
  14.     i += 1
  15. print "-" * 50
  16.  
  17. raw_input('Press ENTER to continue...')
  18.  
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

Share this Question
Share on Google+
4 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
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
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

bvdet
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
  1. x = int(raw_input('What size multiplication table would you like: '))
  2. for row in range(1, x + 1):
  3.     print "|".join(["%4d" % (row), "".join(["%4d" % (row*col) for col in range(1, x + 1)])])
  4.  
  5.  
The correct output is suppose to look something like this:
Expand|Select|Wrap|Line Numbers
  1.  |   1   2   3   4   5
  2. ----------------------
  3. 1|   1   2   3   4   5
  4. 2|   2   4   6   8  10
  5. 3|   3   6   9  12  15
  6. 4|   4   8  12  16  20
  7. 5|   5  10  15  20  25
  8.  
Nov 5 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.