Printing a multipication table from user input

Click For Summary
SUMMARY

The discussion focuses on creating a Python script to generate a multiplication table based on user input for the starting row and column values. The user successfully implemented row headers but struggled with column headers. The solution involves placing the print statement for column headers before the row iteration loop. This ensures that the headers are printed correctly before the multiplication values.

PREREQUISITES
  • Basic understanding of Python programming
  • Familiarity with loops in Python
  • Knowledge of user input handling in Python
  • Experience with formatted string output in Python
NEXT STEPS
  • Learn about Python's string formatting techniques
  • Explore nested loops in Python for complex data structures
  • Research best practices for user input validation in Python
  • Study the use of functions to modularize Python scripts
USEFUL FOR

Python developers, students learning programming, and anyone interested in automating the generation of formatted output in Python scripts.

mr.me
Messages
49
Reaction score
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
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.
 

Similar threads

Replies
7
Views
3K
  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 5 ·
Replies
5
Views
6K
  • · Replies 3 ·
Replies
3
Views
3K
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
7K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 6 ·
Replies
6
Views
5K
Replies
9
Views
2K