Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Printing a multipication table from user input

  1. Nov 16, 2011 #1
    My assignment was to "create a Python script that will allow the user to input the starting value for the row and column of a multiplication table and then print to screen the table containing ten columns and ten rows. Include a header for each row and column"

    I was able to produce the calculated table and I was able to produce a header for each row, but not each column, I do not know where to put the print statement for such a thing, when I fiddle with it always prints incorrectly.

    Any suggestions ?

    The attempt at a solution__

    Code (Text):

    row = int(raw_input("Enter the first row number: "   ))    
    #These 2 lines prompt the user for input and store the value in "row" and "col"
    col = int(raw_input("Enter the frist column number: "))

    lastRow = row + 10               # These lines assign and store the value  of the variables
    firstCol = col
    lastCol = col + 10

    while (row < lastRow):         #This line creates the conditions in which both loops operate            
        print row,"|",                   #Prints a header for each row
        while(col < lastCol):         #This line creates the conditions for the inner loop
            print "%3d" %(col * row), #This line will calculate /print the values of the variables
            col +=1                           #This line  updates the value for which "col"
        col = firstCol
        row +=1                         #This line updates the value for "row"
  2. jcsd
  3. Nov 17, 2011 #2


    Staff: Mentor

    The header for the columns should be printed before you print rows of the table. IOW, the print statement(s) should go above your while loop.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook