Python multiplication table and user input

In summary: Outer Row: " + str(lastRow) + " - " + str(firstCol)) print("\n Outer Column: " + str(lastCol) + " - " + str(firstCol))
  • #1
mr.me
49
0

Homework Statement


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:
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
 
Technology news on Phys.org
  • #2
What is initial col value when you enter the inner loop for the second time?
 
  • #3
I suppose there isn't an initial value for the second loop, what would it need to be?
 
  • #4
Draw the table manually (5x5 will be enough) and see what value do you use for calculations.
 
  • #5
Borek said:
Draw the table manually (5x5 will be enough) and see what value do you use for calculations.
Excellent advice!

In a CS class I took once, the instructor related this adage to us.
The sooner you sit down to the keyboard, the longer it will take to write your program.

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.
 
  • #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:
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
 

1. How can I create a multiplication table in Python?

To create a multiplication table in Python, you can use a nested for loop. The outer loop will iterate through the rows, and the inner loop will iterate through the columns. Within the inner loop, you can use the multiplication operator (*) to calculate the product of the row number and column number.

2. How do I take user input for the size of the multiplication table?

To take user input for the size of the multiplication table, you can use the input() function in Python. This function prompts the user to enter a value, which can then be stored in a variable. You can use this variable to specify the size of the multiplication table.

3. Can I customize the format of the multiplication table in Python?

Yes, you can customize the format of the multiplication table in Python. You can use string formatting to control the appearance of the table. For example, you can use the format() method to specify the number of digits after the decimal point or to left justify the numbers.

4. How can I handle errors if the user enters a non-numeric value?

To handle errors if the user enters a non-numeric value, you can use the try-except statement in Python. Within the try block, you can use the int() function to convert the user input into an integer. If the user enters a non-numeric value, an error will occur, which can be caught in the except block.

5. Is there a limit to the size of the multiplication table that can be created in Python?

There is no specific limit to the size of the multiplication table that can be created in Python. However, the size of the table may be limited by the memory and processing power of your computer. If you are creating a very large table, it may take longer to run and may require more memory.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
7
Views
451
  • Programming and Computer Science
Replies
22
Views
4K
  • Programming and Computer Science
Replies
29
Views
1K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
4
Views
920
Replies
5
Views
891
  • Programming and Computer Science
Replies
4
Views
358
  • Programming and Computer Science
Replies
8
Views
888
Back
Top