Python multiplication table and user input

Click For Summary

Discussion Overview

The discussion revolves around a homework problem involving the creation of a multiplication table in Python. Participants are exploring issues related to user input, iteration, and output formatting for a table of 10 positive integers both across the columns and down the rows.

Discussion Character

  • Homework-related
  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant expresses confusion about their code's output format and the order of statements needed to achieve a grid layout for the multiplication table.
  • Another participant questions the initial value of the column variable when entering the inner loop for the second time.
  • A suggestion is made to manually draw a smaller version of the table (5x5) to clarify the values used in calculations.
  • A later reply emphasizes the importance of planning the algorithm before coding, citing an instructor's advice on programming efficiency.
  • A participant shares revised code but indicates a need for further adjustments to include an outer row and column that displays the values being multiplied.

Areas of Agreement / Disagreement

Participants do not appear to reach a consensus on the best approach to formatting the output or the specific logic needed for the code. Multiple viewpoints and suggestions are presented without resolution.

Contextual Notes

There are limitations in the provided code regarding the initialization of variables and the structure of nested loops, which may affect the output format. The discussion does not resolve these issues.

mr.me
Messages
49
Reaction score
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
What is initial col value when you enter the inner loop for the second time?
 
I suppose there isn't an initial value for the second loop, what would it need to be?
 
Draw the table manually (5x5 will be enough) and see what value do you use for calculations.
 
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.
 
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
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
2K
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 22 ·
Replies
22
Views
5K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K