2017-06-10 25 views

回答

-1

您還沒有宣佈和變量的類型,也propably你要分配陣列通過你的描述使用圖像您沒有給我們提供了一種一倍

+0

你好Michal,你的答案是錯誤的,因爲sum變量只存儲兩個數的乘積(即。sum = 3 * 4),所以爲什麼需要將數組賦給sum變量。 –

+0

是不是 'a = [[],[]]' 聲明一個數組數組? –

0

由於複製粘貼你的代碼,所以我從頭重寫了它:詢問他們的內容之前

#!/usr/bin/env python3 

matrixes_names = ('first', 'second') 

def asks_matrix_size(which_one): 
    string_answer = input("Enter the number of rows and the number of colums (space separated) of the {} matrix: ".format(which_one)) 
    answer = tuple(int(x) for x in string_answer.split()) 

    return answer 

matrixes_size = [] 
matrixes_size.append(asks_matrix_size(matrixes_names[0])) 

matrixes = [[], []] 

for row_being_filled in range(matrixes_size[0][0]): 
    row_received = input("Enter the {} elements (space separated) of row number {} of the {} matrix: ".format(matrixes_size[0][1], 
                              row_being_filled + 1, 
                              matrixes_names[0])) 
    row = [int(x) for x in row_received.split()] 
    matrixes[0].append(row) 

matrixes_size.append(asks_matrix_size(matrixes_names[1])) 

if matrixes_size[1][0] == matrixes_size[0][1]: 
    matrixes[1] = [[] for _ in range(matrixes_size[1][1])] 

    for row_being_filled in range(matrixes_size[1][0]): 
     row_received = input("Enter the {} elements (space separated) of row number {} of the {} matrix: ".format(matrixes_size[1][1], 
                               row_being_filled + 1, 
                               matrixes_names[1])) 
     row = [int(x) for x in row_received.split()] 
     for (column, row_transposed) in zip(row, matrixes[1]): 
      row_transposed.append(column) 

    solution_matrix = [] 

    for row_from_first in matrixes[0]: 
     solution_row = [] 
     for column_from_second in matrixes[1]: 
      solution_row.append(sum(x*y for (x, y) in zip(row_from_first, column_from_second))) 
     solution_matrix.append(solution_row) 

    print(solution_matrix) 
else: 
    print("Matrix multiplication is not possible") 

我們可以進一步凝聚了解決方案,如果我們被允許索要兩個矩陣的大小:

#!/usr/bin/env python3 

matrixes_names = ('first', 'second') 

def asks_matrix_size(which_one): 
    string_answer = input("Enter the number of rows and the number of colums (space separated) of the {} matrix: ".format(which_one)) 
    return tuple(int(x) for x in string_answer.split()) 

matrixes_size = [asks_matrix_size(x) for x in matrixes_names] 

if matrixes_size[1][0] == matrixes_size[0][1]: 

    def ask_matrix_elements(size, matrix_name): 
     print("") 
     for row_being_filled in range(size[0]): 
      row_received = input("Enter the {} elements (space separated) of row number {} of the {} matrix: ".format(size[1], 
                               row_being_filled + 1, 
                               matrix_name)) 
      yield tuple(int(x) for x in row_received.split()) 

    matrixes = [tuple(ask_matrix_elements(x, y)) for (x, y) in zip(matrixes_size, matrixes_names)] 

    # Transposing the second matrix (to simplify the computation of the solution) 
    matrixes[1] = tuple(zip(*matrixes[1])) 

    solution_matrix = tuple(tuple(sum(x*y for (x, y) in zip(row_from_first, column_from_second)) 
            for column_from_second in matrixes[1]) 
          for row_from_first in matrixes[0]) 

    print(solution_matrix) 
else: 
    print("Matrix multiplication is not possible")