• Support PF! Buy your school textbooks, materials and every day products Here!

Python multiplication table and user input

  • Comp Sci
  • Thread starter mr.me
  • Start date
  • #1
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
 

Answers and Replies

  • #2
Borek
Mentor
28,352
2,738
What is initial col value when you enter the inner loop for the second time?
 
  • #3
49
0
I suppose there isn't an initial value for the second loop, what would it need to be?
 
  • #4
Borek
Mentor
28,352
2,738
Draw the table manually (5x5 will be enough) and see what value do you use for calculations.
 
  • #5
33,262
4,962
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
49
0
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
 

Related Threads for: Python multiplication table and user input

Replies
1
Views
2K
Replies
11
Views
1K
  • Last Post
Replies
4
Views
7K
Replies
0
Views
3K
Replies
9
Views
7K
Replies
2
Views
1K
Replies
0
Views
2K
Top