使用冒号与缩进定义代码块时遭遇编译错误的技术咨询
问题描述
- 编译错误1:提示存在多余输入
:,期望end of line without line continuation - 编译错误2:尝试用冒号
:+缩进定义代码块无效,且出现第10行输入then不匹配、期望end of line without line continuation的错误
用户提供的代码示例:
// Define the trend based on the position of the moving average if ema > ema[1]: trend = 'Up' elif ema < ema[1]: trend = 'Down' else: trend = 'Sideways'
解决方案
你写的是Python风格的条件判断语法,但当前使用的语言(大概率是VBA或类似需要显式闭合代码块的语言)不支持这种写法,需调整为对应语法规范:
- 条件判断语句结尾用
Then替代冒号: - 用
ElseIf替代elif(部分语言对大小写敏感,需注意匹配语言要求) - 代码块无需靠缩进区分,用
End If显式闭合整个条件结构 - 字符串改用双引号
""包裹(部分语言强制要求字符串使用双引号) - 数组索引写法调整为
ema(1)(部分语言不支持方括号索引)
修正后的代码示例:
' Define the trend based on the position of the moving average If ema > ema(1) Then trend = "Up" ElseIf ema < ema(1) Then trend = "Down" Else trend = "Sideways" End If
内容的提问来源于stack exchange,提问作者Niran Nguyễn
相关产品推荐
相关产品推荐

