1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Python multiplication table and user input

  1. Nov 3, 2011 #1
    1. The problem statement, all variables and given/known data
    I'm having and issue with this problem, and I don't understand what I'm doing wrong, I really want to understand the material

    Write code that will print a multiplication table for 10 positive integers across the columns and 10 positive integers down the rows. Prompt the user for the starting values for both the columns and the rows.

    3. The attempt at a solution

    Code (Text):

    row = int(raw_input("Enter the first row number: "   ))
    col = int(raw_input("Enter the first column number: "))

    lastRow = row + 10
    lastCol = col + 10


    while (row < lastRow):
        print "%4d" % (col * row)
        while(col < lastCol):
                   
            print "%4d" % (col * row),
            col += 1
           
        print "%4d" % (col * row)
        row += 1
     
    I believe my iterations are correct but I don't get the desired print out in grid format, I am unsure as to what the order of the statements should be
     
  2. jcsd
  3. Nov 3, 2011 #2

    Borek

    User Avatar

    Staff: Mentor

    What is initial col value when you enter the inner loop for the second time?
     
  4. Nov 3, 2011 #3
    I suppose there isn't an initial value for the second loop, what would it need to be?
     
  5. Nov 3, 2011 #4

    Borek

    User Avatar

    Staff: Mentor

    Draw the table manually (5x5 will be enough) and see what value do you use for calculations.
     
  6. Nov 3, 2011 #5

    Mark44

    Staff: Mentor

    Excellent advice!

    In a CS class I took once, the instructor related this adage to us.
    That might seem counterintuitive to new programmers, but people who have been writing code for a while realize that putting a reasonable amount of thought into the algorithm beforehand saves a lot of time.
     
  7. Nov 3, 2011 #6
    Thanks everyone

    Here's my new code which works except I need it to print an outer row and column that shows each value that is multiplied I don't know where to place the print statement for the iterative it keeps messing things up i.e. if entered 1 and 1 it should look like this, and
    http://www.mathsisfun.com/tables.html

    Code (Text):

    row = int(raw_input("Enter the first row number: " ))
    col = int(raw_input("Enter the frist column number: "))
         
    lastRow = row + 10
    firstCol = col
    lastCol = col + 10
         
         
    while (row < lastRow):
         
        while(col < lastCol):
         
         
              print "%4d" % (col * row),
              col += 1
         
        col = firstCol
        row += 1
        print
     
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Python multiplication table and user input
Loading...