YouTip LogoYouTip

Python Exercise Example38

# Python2.x Python Exercise 38 [![Image 3: Python 100 Examples](#) Python 100 Examples](#) **Question:** Calculate the sum of the main diagonal elements of a 3*3 matrix. **Program Analysis:** Use a double for loop to control the input of the two-dimensional array, then accumulate a and output the result. Program source code: ## Example ```python #!/usr/bin/python # -*- coding: UTF-8 -*- if __name__ == '__main__': a = [] sum = 0.0 for i in range(3): a.append([]) for j in range(3): a.append(float(input("input num:n"))) for i in range(3): sum += a print(sum) The output result of the above example is: input num: 78 input num: 34 input num: 23 input num: 34 input num: 56 input num: 33 input num: 12 input num:
← Python Exercise Example39Python Exercise Example37 β†’