473,396 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to add " | " to a mutliplication table?

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

4 3159
bvdet
2,851 Expert Mod 2GB
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
Fuugie
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
2,851 Expert Mod 2GB
The same principles will apply.
Nov 5 '10 #4
Fuugie
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

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

Similar topics

3
by: Dam | last post by:
Using SqlServer : Query 1 : SELECT def.lID as IdDefinition, TDC_AUneValeur.VALEURDERETOUR as ValeurDeRetour FROM serveur.Data_tblDEFINITIONTABLEDECODES def,...
6
by: TS | last post by:
Hi, i have a problem validating xml against schema. I used http://apps.gotdotnet.com/xmltools/xsdvalidator/Default.aspx validator and it says it is fine. Can you tell me why this doesn't work? ...
81
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
1
by: Benny Ng | last post by:
Hi,All, Export Method: ------------------------------------------------------------------------- strFileNameExport = "Results" Response.Clear() Response.Buffer = True...
6
by: Rich | last post by:
Hello, I have to create a table in an Access mdb (remotely) on the fly. Create Table tbl1(fld1 Integer, fld2 varchar(10), fld3...) Then I have to insert data: Insert Into tbl1 Values(" &...
11
by: MLH | last post by:
DLookup("", "tblPreliminaryVINs", "=Forms!frmVINODO!SerialNum") is giving me a Type Mismatch error. That's confusing to me and I don't know how to circumvent it. The field in...
43
by: markryde | last post by:
Hello, I saw in some open source projects a use of "!!" in "C" code; for example: in some header file #define event_pending(v) \ (!!(v)->vcpu_info->evtchn_upcall_pending & \...
1
by: manchin2 | last post by:
Hi, Can anybody please provide the information about "&quot" and its use, if possible please provide an example. ...
4
by: fran7 | last post by:
Hi, from help in the javascript forum I found the error in some code but need help. This bit of code works perfectly, trouble is I am writing it to a javascript function so the height needs to be in...
4
by: dougmeece | last post by:
Hello everyone, I have created a search button but I receive the message "Microsoft Access can't fine the field "|" referred to in your expression" when I try to use it. I have looked online for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.