I have been working with python for about a month now, but today I came across a problem I have yet to see; multiplication tables. I have absolutely no idea where to start or how to begin to format said table. The problem will be listed below, and if anyone can help getting me started I would be greatly thankful.

-Tyler

Write a program to display a multiplication table for integers in a given range. The program should accept two integers as input representing the minimum and maximum values the table should contain. The program should then print a table representing the multiplication table for integers in the given range. Note that you need to define two functions in this program. First function get_integers() gets inputs from the user; it takes no values, and returns two integers (the second one is larger than the first one). Second function display_table() displays the multiplication table, which takes two integers as its parameters. See a sample table below (the user types in 3 and 8):

|    3    4    5    6    7    8 
-----------------------------------
   3|    9   12   15   18   21   24 
-----------------------------------
   4|   12   16   20   24   28   32 
-----------------------------------
   5|   15   20   25   30   35   40 
-----------------------------------
   6|   18   24   30   36   42   48 
-----------------------------------
   7|   21   28   35   42   49   56 
-----------------------------------
   8|   24   32   40   48   56   64

Note for this program, the easiest way to print out the table with the numbers aligned as the one shown above is to use string formatting. The syntax of string formatting is: format % values. For examples, "%4d" % 3 gives ' 3' (notice the 3 spaces before number 3), and "%4d%4d" % (3, 4) gives ' 3 4'. In these two examples, %4d ('%' character marks the start of the specifier) tells the program to convert an integer (what d stands for) into a string by inserting spaces before the number to make a string with length of 4 (what 4 stands for).

Recommended Answers

All 5 Replies

Here's a little help to get the juices flowing, but for the future you need to show effort around here take heed of Ezzaral's edit notes and read that thread, or for a quicker review of what he's talking about read pytony's signature

def mult_table_numbers():
    alpha = int(input('Starting: '))
    omega = int(input('Ending: '))
    for intg in range(alpha, omega+1):
        for i in range(alpha,omega+1):
            print(str(intg)+'x'+str(i)+'='+str(intg*i))

this is what I have so far. I am stuck a little bit, I have been able to create the table, but it is not reading the specs of the rows and columns. any ideas?

#Multiplication Table

def get_integers():
    row = raw_input("Enter a number for the row:")
    column = raw_input("Enter a number for the column:")
    display_table()
def display_table():
    for i in range(row + 1):
        for j in range(column + 1):

            if (i == 0 and j == 0) :
                print "\t*",

            elif (i == 0) :
                print "\t", j,

                if (j == column):
                    print "\n"

            elif (j == 0) :
                print "\t", i,

            else :
                print "\t", i * j,
get_integers()

raw_input("Press any key to quit.")

sorry messed up the code pastings.

#Multiplication Table
def get_integers():
    row = raw_input("Enter a number for the row:")
    column = raw_input("Enter a number for the column:")
    display_table()
def display_table():
    for i in range(row + 1):
        for j in range(column + 1):

            if (i == 0 and j == 0) :
                print "\t*",
            
            elif (i == 0) :
                print "\t", j,

                if (j == column):
                    print "\n"
                
            elif (j == 0) :
                print "\t", i,
 
            else :
                print "\t", i * j,
get_integers()

raw_input("Press any key to quit.")

row and column values set in get_integers are never used.

Your first problem is you didn't pass your variables row and column into your display_table() like display_table(row, column). Your next issue is in your loops but you will see that when you pass the variables to the function. Also the problem suggests using string formatting to position the text not using '\t's for tabbing.


I can show you what I got using python 3.2, I see you are still using a python 2.x version so this won't work without changes for you.

#Multiplication Table

def get_integers():
    rows = input("Enter a number for the rows: ")           #get rows
    columns = input("Enter a number for the columns: ")     #get columns
    display_table(rows, columns)                            #pass rows and columns to display_table
    
def display_table(rows, columns):
    rows = int(rows)                                        #convert rows to an int
    columns = int(columns)+1                                #convert columns to an int but increase by 1 because loops don't include the last nubmer
    length = 5*(columns-rows+1)                             #length is the number of '-'s used when making a dashed line between rows the + 1 is because of the far left column
    
    
    #print the first row (the one with no multiplication occurring)
    print("{:>4}|".format(""), end="")
    for column in range(rows, columns):
        print("{:>5}".format(str(column)), end="")
    print()
    
    #print the remaining rows
    for row in range(rows, columns):
        print("-"*length)                                   #print a line of dashes '-'
        print("{:>4}|".format(row), end="")                 #print the first number (the one not being multiplied)
        for column in range(rows, columns):
            print("{:>5}".format(row*column), end="")       #loop through the columns and multiple it by the the current row
        print()                                             #finally print a newline
    print()
    
    
    
#the main function
def main():
    get_integers()
    input("Press any key to quit.")

#this is just the syntax for running a main()
if __name__ == "__main__":
    main()

For the example given it's output is this:

Enter a number for the rows: 3
Enter a number for the columns: 8
    |    3    4    5    6    7    8
-----------------------------------
   3|    9   12   15   18   21   24
-----------------------------------
   4|   12   16   20   24   28   32
-----------------------------------
   5|   15   20   25   30   35   40
-----------------------------------
   6|   18   24   30   36   42   48
-----------------------------------
   7|   21   28   35   42   49   56
-----------------------------------
   8|   24   32   40   48   56   64

Press any key to quit.

You will have to change the print statements in order to get it to work with python 2. You can look up converting python 3 to 2 or something on google if you need help with the difference between python 2 and 3.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.