【问题标题】:Simple for loop doesen't work in python简单的for循环在python中不起作用
【发布时间】:2016-01-31 08:05:23
【问题描述】:

我有一个矩阵(称为 configmatrix),并且我在 python 中编写了一个 for 循环来求和它的元素。这是我的代码:

def magnetic (l):
M = 0
for i in range (l):                           
    for j in range (l):
        M = M + configmatrix[i][j]
    return M

但我收到此错误消息: 我不知道我的代码有什么问题。

【问题讨论】:

  • 你的函数定义没有缩进。
  • 错误信息再清楚不过了...
  • 可能是 Python 中的一个错误。有没有人检查过for 今天是否还在工作?

标签: python for-loop


【解决方案1】:

你需要像这样缩进你的函数体:

 def magnetic (l):
    M = 0
    for i in range (l):                           
        for j in range (l):
            M = M + configmatrix[i][j]
    return M

【讨论】:

    猜你喜欢
    • 2015-02-22
    • 2011-04-08
    • 1970-01-01
    • 1970-01-01
    • 2014-01-15
    • 1970-01-01
    • 2018-09-09
    • 2014-07-28
    • 1970-01-01
    相关资源
    最近更新 更多