This is mine, but it doesn\'t run. So I need help... This is python 3. def sumCo
ID: 3662527 • Letter: T
Question
This is mine, but it doesn't run. So I need help... This is python 3.
def sumColumn(matrix, columnIndex):
sum = 0
for i in range(lne(matrix)):
sum += row[columnIndex]
return sum
def getMatrix():
matrix = []
for row in range(3):
print("Enter a 3-by-4 matrix row for now: %d" %row)
row = input().split(' ')
row = [float(i) for i in row]
matrix.append(row)
return matrix
def display(x):
for row in x:
print(row)
def main():
m = getMatrix()
print("The matrix is")
display(x)
print()
for column in range(len(x[0])):
print("Sum of elements for column %d is %.2f" % (column, sumColumn(x, column)))
main()
This is a question.
Write a function that returns the sum of all the elements in a specified column in a matrix using the following header
def sumColumn(matrix, columnIndex)
Write a function that displays the elements in a matrix row by row, where the values in each row are displayed on a separate line (see the output below). The format of your output must match that in the sample output where the values on a row are separated by a single space.
Write a test program (i.e., a main function) that reads a 3 X 4 matrix and displays the sum of each column. The sum should be formatted to contain exactly one significant digit after the decimal point. The input from the user must be entered as in the sample program run below where the input is read row by row and values in a row are separated by single space.
Explanation / Answer
Ok. Here is the code for you. If you have any further queries, just get back to me.
#!/usr/bin/python
def sumColumn(matrix, columnIndex):
s = 0
for i in range(len(matrix)):
s = s + matrix[i][columnIndex]
return s
def getMatrix():
matrix = []
for row in range(3):
print("Enter a 3-by-4 matrix row for now: %d" %row)
row = raw_input().split(' ')
row = [float(i) for i in row]
matrix.append(row)
return matrix
def display(x):
for row in x:
print(row)
def main():
m = getMatrix()
print("The matrix is")
display(m)
for column in range(len(m[0])):
print("Sum of elements for column %d is %.2f" % (column, sumColumn(m, column)))
main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.