Write a Python program to read a matrix from console and print the sum for each column. Accept matrix rows, columns and elements for each column separated with a space(for every row) as input from the user

  • برمجة
  • برمجة بايثون

Write a Python program to read a matrix from console and print the sum for each column. Accept matrix rows, columns and elements for each column separated with a space(for every row) as input from the user

الأجوبة

rows = int(input("Input rows: "))
columns = int(input("Input columns: "))
matrix = [[0]*columns for row in range(rows)]
print('Input number of elements in a row (1, 2, 3): ')
for row in range(rows):
    lines = list(map(int, input().split()))
    for column in range(columns):
        matrix[row][column] = lines[column]

sum = [0]*columns
print("sum for each column:")
for column in range(columns):
    for row in range(rows):
        sum[column] += matrix[row][column]
    print((sum[column]), ' ', end = '')
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...