I'm trying to make a header for my table in Python but the numbers that identify each column won't align with the columns in my table -


this program simple. in program, i'm making power table based on number of rows number of columns user puts in. instance, if user puts in 5 amount of rows, , 6 amount of columns, table appear headers telling how many rows there , how many columns there are, this:

power table python

with code have now, power table works header columns @ top not aligned, though made them same spacing. plus, need underscores under column headers shown above. here code:

def main():     inputrows= 0     inputcolumns= 0       while (inputrows < 2) or (inputrows > 12):         inputrows= int(input("enter integer value rows between 2 , 12: "))       while (inputcolumns < 2) or (inputcolumns > 12):         inputcolumns= int(input("enter integer value columns between 2 , 12: "))     powertable= buildtable(inputcolumns, inputrows, inputcolumns)        in range(inputrows):         print(i, end="|")         j in range(inputcolumns):             powertable[i][j]= i**j             print("%8d" % powertable[i][j], end= "|")         print()   def buildtable(inputcolumns, rows, cols) :     table = []                  # initialize tabl     in range(rows) :      # each row         row = [0] * cols         # create row cols values         table.append(row)        # , append end of table     in range(inputcolumns):         print("%8d" % i, end="|")     print()     return table  main() 

thank suggestions.

to create table of dimension rows x cols, values set zero, can do:

def build_table(rows, cols):     """ create table of rows x cols values set 0. """     return [[0] * cols _ in range(rows)] 

for instance:

>>> build_table(3, 5) [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]] 

now, if want create multiplication table, can follow:

def build_mul_table(rows, cols):     """ calculate *mul* table. """     return [[i * j j in range(1, cols + 1)] in range(1, rows + 1)] 

this equivalent to:

table = [] in range(1, rows + 1):     row = []     j in range(1, cols + 1):         row.append(i * j)     table.append(row) 

for instance:

>>> build_mul_table(3, 5) [[1, 2, 3, 4, 5], [2, 4, 6, 8, 10], [3, 6, 9, 12, 15]] 

then, can write separate function display kind of table:

def print_table(table):     cols = len(table[0])     header = ["    "] + ["{0:4d}".format(j) j in range(1, cols + 1)]     print(" | ".join(header))     header = ["----"] + ["----" j in range(1, cols + 1)]     print(" + ".join(header))     i, row in enumerate(table, 1):         line = ["{0:4d}".format(i)]  + ["{0:4d}".format(v) v in row]         print(" | ".join(line)) 

the result:

print_table(build_mul_table(3, 5))       |    1 |    2 |    3 |    4 |    5 ---- + ---- + ---- + ---- + ---- + ----    1 |    1 |    2 |    3 |    4 |    5    2 |    2 |    4 |    6 |    8 |   10    3 |    3 |    6 |    9 |   12 |   15 

Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -