How to add " | " to a mutliplication table?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Fuugie
    New Member
    • Sep 2010
    • 32

    How to add " | " to a mutliplication table?

    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.

    Code:
    '''
    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...')
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    I think this will do what you want:
    Code:
    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:
    Code:
    strlist = []
    for n in range(2,11):
        strlist.append("%4d" % (i*n))
    HTH

    Comment

    • Fuugie
      New Member
      • Sep 2010
      • 32

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

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        The same principles will apply.

        Comment

        • Fuugie
          New Member
          • Sep 2010
          • 32

          #5
          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.
          Code:
          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:
          Code:
           |   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

          Comment

          Working...