Printing a multipication table from user input

In summary, the user is prompted to input the starting value for the row and column of a multiplication table. The script then calculates and prints a table with ten columns and ten rows, including a header for each row. However, the header for each column is not printing correctly and the user is seeking suggestions for a solution.
  • #1
mr.me
49
0
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:
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"
    print
 
Physics news on Phys.org
  • #2
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.
 

1. How do I print a multiplication table from user input?

To print a multiplication table, you will need to use a loop to iterate through the numbers from 1 to the user's input. Within the loop, you can use another loop to iterate through the numbers from 1 to 10 (since multiplication tables typically go up to 10). Each time the inner loop runs, you can print out the product of the two numbers, which will create a multiplication table.

2. What is the purpose of printing a multiplication table from user input?

The purpose of printing a multiplication table from user input is to provide a visual representation of the multiplication facts for a specific number. It can be useful for students learning multiplication or for anyone who needs to quickly reference multiplication facts.

3. How can I customize the appearance of the multiplication table?

You can customize the appearance of the multiplication table by using formatting options such as spacing, font, and color. You can also add a title or headers to make it more organized and visually appealing.

4. Can I print a multiplication table for numbers other than 1-10?

Yes, you can print a multiplication table for any set of numbers. You will just need to adjust the range in your loops to match the desired numbers. For example, if you want to print a multiplication table for numbers 1-12, you will need to change the range in your inner loop to go up to 12 instead of 10.

5. Is there a limit to the size of the multiplication table I can print?

The size of the multiplication table you can print may depend on the limitations of your computer's memory. However, as long as your computer has enough memory, you can theoretically print a multiplication table for any range of numbers.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
5
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
Back
Top