python - Reading Two Input Files -
i have following problem statement:
write program that:
- reads 2 inputs files
- populates 2 dimensional table integers contained in each file
- check size of 2 tables make sure both have same number of rows , columns. if tables not same size print error message
- once have read data each file create third table
- the elements in third table result of multiplying each element in first table corresponding element in second table:
thirdtable [i] [j] = firsttable[i] [j] * secondtable[i] [j]
i need know how put second file in code far.and how write code table three. here code first input file:
def main(): print("table one") (row, column, table) = readinput() return() def readinput(): table = [] inputfile = open("table 1.txt", "r") # read first line containing number of rows , columns line = inputfile.readline() # split line 2 strings (row, column) = line.split() # convert strings integers row = int(row) column = int(column) # loop on file container, reading each line line in inputfile : line = line.rstrip() #strip off newline datalist = line.split() # split string list table.append(datalist) # loop through table , convert each element integer in range(row): j in range (column): table[i] [j] = int(table[i] [j]) # convert string integer print(" %3d" % (table[i] [j]), end = " ") print() inputfile.close() # close file return(row, column, table) # return number of rows , columns main()
you can make readinput
function take parameter:
def readinput(filename): # ^^^^^^^^ change here table = [] inputfile = open(filename, "r") # , here ^^^^^^^^ ... # rest of function
then use main
this:
def main(): row1, column1, table1 = readinput('table1.txt') row2, column2, table2 = readinput('table2.txt')
Comments
Post a Comment