【问题标题】:How can I deal with IndentationError: unexpected indent that is not caused by white space?如何处理 IndentationError: 非空白引起的意外缩进?
【发布时间】:2019-06-09 10:02:04
【问题描述】:

我正在尝试将 MSE 函数从 Matlab 转换为 Python,但出现此错误:

 for i in range(len(RuleBase)):
^
IndentationError: unexpected indent

我已经尝试过使用空格,除了空格还有什么问题?

这是我在 matlab 中的函数:

function err=Mse(RuleBase,x1,x2)
temp=zeros(1,6);
Soogeno=zeros(49,4);
for i=1:length(RuleBase)
    y=crisp(0,50,RuleBase(i,3),7);
    temp(1,:)=RuleBase(i,:);
    temp(1,3)=y;
    Soogeno(i,:)=temp(1,1:4);    
end

这是我的python代码:

def Mse(RuleBase,x1,x2):
temp=np.zeros(shape = (1,6))
soogeno=np.zeros(shape = (49,4))

for i in range(len(RuleBase)):
    y=crisp(m=0,M=50,fy=RuleBase[i,3],n=7) 
    temp[0]=RuleBase[i]
    temp[0,2]=y
    Soogeno[i]=temp[0,0:3]
    return(soogeno)

【问题讨论】:

  • 谷歌如何编写for循环以及如何在python中编写函数定义。空格很重要,你的错误是说你在 Python 不希望找到的地方缩进(例如放一个制表符)。

标签: python arrays matlab fuzzy-logic mse


【解决方案1】:

缩进在python中是严格执行的:

这应该运行:

def Mse(RuleBase,x1,x2):
    temp=np.zeros(shape = (1,6))
    soogeno=np.zeros(shape = (49,4))

    for i in range(len(RuleBase)):
        y=crisp(m=0,M=50,fy=RuleBase[i,3],n=7)
        temp[0]=RuleBase[i]
        temp[0,2]=y
        Soogeno[i]=temp[0,0:3]
    return(soogeno)     

【讨论】:

  • 在任何函数定义 def Mse() 、if 语句或 for 循环等之后,根据官方 python 指南 (PEP 8),您在下一行 () 中包含 4 个空格。
  • @NargesSe 如果它有效并解决了您的问题,请将答案标记为已接受(单击旁边的勾号)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-04
  • 2022-08-20
  • 2011-04-24
  • 1970-01-01
  • 2012-05-01
  • 1970-01-01
相关资源
最近更新 更多