У меня есть следующая проблема заявление:Чтение два входных файлов
Write a program that:
- Reads two inputs files
- Populates a two dimensional table with the integers contained in each file
- Check the size of the two tables to make sure they both have the same number of rows and columns. 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]
Мне нужно знать, как поставить второй файл с моим кодом так far.And, как писать код для таблицы три. Вот код для первого входного файла:
def main():
print("Table One")
(row, column, table) = readInput()
return()
def readInput():
table = []
inputFile = open("Table 1.txt", "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()
Является ли это Python? Добавьте тег языка. –
@YuHao это python – lindsay
'с открытым (file1) как f1, open (file2) как f2:' приходит на ум. Использование с блоками для обработки IO в основном предпочтительнее –