python - Python 复杂 IF 语句
问题描述
我在这里遇到一些语法错误,似乎找不到它:
if open_prices[0] > close_prices[0]:
if (close_prices[0] - low_prices[0]) >= 2*(open_prices[0] - close_prices[0]) and
(high_pices[0] - open_prices[0]) <= 0.5*(open_prices[0] - close_prices[0]):
x = 1
错误是
if (close_prices[0] - low_prices[0]) >= 2*(open_prices[0] - close_prices[0]) and (high_pices[0] - open_prices[0] <= 0.5*(open_prices[0] - close_prices[0]):
^
SyntaxError: invalid syntax
我的语法或格式不正确吗?
解决方案
您的第二个 if 语句结构错误。Python 遵循严格的缩进规则,当它不知道如何处理一个语句时会抛出一个语法错误,这里你已经留下了一个部分(以 结尾的行and
),所以它试图将它解析为一个完整的语句,这显然失败。
一个简单的解决方法是将整个谓词括在括号内(这使其成为单个语句),如下所示:
if ( (close_prices[0] - low_prices[0]) >= 2*(open_prices[0] - close_prices[0]) and
(high_pices[0] - open_prices[0]) <= 0.5*(open_prices[0] - close_prices[0]) ):
x = 1
推荐阅读
- pointers - 在 vbscript 中打开窗口对话框后如何更改变量?
- r - 如何使用 R 在另一列上保留以前的 id 条件?
- python - 在python中将数据添加到html变量
- python-3.x - 当并行度设置为很大时,气流调度程序启动异常
- ios - 如何在swift中使用视觉框架从其特征点中提取外唇
- c - 如何将此 C 函数指针转换为 delphi 函数
- c# - ASP.Net 页面第一次运行缓慢如果我使用 System.Web.Ui 命名空间
- html - Angular 6 bootstrap 4 - 导航栏品牌标志在向下滚动时不会改变
- html - 如何操作 trix-editor 在连续输入 4 次后停止调整大小
- typescript - 为什么 Typescript 代码是(或不是)循环依赖?