[Tutor] Input Files

niyanaxx95 at gmail.com niyanaxx95 at gmail.com
Wed Nov 19 16:21:58 CET 2014


How do I know write the loop code for the table for both inputfiles, Table1.txt and Table2.txt and make a third table from the elements multiplied in table1.txt and table2.txt. I'm trying to Check the size of the two tables to make sure they both have the same number of rows and columns o If the tables are not the same size print an error message  Once you have read the data from each file create a third table  The elements in the third table are the result of multiplying each element in the first table by the corresponding element in the second table: 
thirdTable [i] [j] = firstTable[i] [j] * secondTable[i] [j


Here is my code so far:
    
    def main():
        print("Table One")
        (row1, column1, table1) = readInput("Table 1.txt")
        print("Table Two")
        (row2, column2, table2) = readInput("Table 2.txt")
        return()



    def readInput(filename):
        table = []
        inputFile = open(filename, "r")


         #  Read the first line containing the number of rows and columns
        line = inputFile.readline()
    
        #  split the line into two strings
        (row, column) = line.split()
    
    
        #  convert the strings into integers
        row = int(row)
        column = int(column)
    
    
        #  loop on the file container, reading each line
    
        for line in inputFile :
            line = line.rstrip()  #strip off the newline 
            dataList = line.split()  #  split the string into a list 
            table.append(dataList)
 
     #  Loop through the table and convert each element to an integer
    
    
        for i in range(row):
            for j in range (column):
                table[i] [j] = int(table[i] [j])  # convert the string to an integer
                print(" %3d" % (table[i] [j]), end = " ")
            print()
    
        inputFile.close()  #  close the file
        return(row, column, table)
   
    #  return the number of rows and columns



    main()    






Sent from Windows Mail
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20141119/3691c2ea/attachment.html>


More information about the Tutor mailing list