修改代码 把a改成动态的import numpy as np def generate_matrix(a): arr = np.zeros((a,a)) for i in range(a): arr[i,:i+1] = np.arange(1,i+2) return arr a = 5 matrix = generate_matrix(a) col_sum = np.sum(matrix, axis=0) output_list = [] for i in range(len(col_sum)): output_list.append(int(col_sum[i])) print('[{}]'.format(' '.join(map(str, output_list))))
时间: 2024-03-04 09:48:33 浏览: 123
python 3.74 运行import numpy as np 报错lib\site-packages\numpy\__init__.py
你需要将函数 generate_matrix 中的参数 a 改成动态的,可以通过输入参数的方式来实现。以下是修改后的代码:
import numpy as np
def generate_matrix(a):
arr = np.zeros((a,a))
for i in range(a):
arr[i,:i+1] = np.arange(1,i+2)
return arr
a = int(input("请输入a的值:"))
matrix = generate_matrix(a)
col_sum = np.sum(matrix, axis=0)
output_list = []
for i in range(len(col_sum)):
output_list.append(int(col_sum[i]))
print('[{}]'.format(' '.join(map(str, output_list))))
阅读全文