【问题标题】:How to handle unreachable code in Python in for loop如何在for循环中处理Python中无法访问的代码
【发布时间】:2019-11-30 00:22:34
【问题描述】:

我正在尝试检查列表中的上升或下降趋势。如果趋势在上升,我希望它打印“Buy”,否则打印“Sell”。

这是一个用于趋势分析的python程序。 PS-我是编码新手

adi = ta.acc_dist_index(high, low, close, volume, fillna=False)
a = adi[-1]

ADI = adi[-7:]
vol_status_adi = ""

if a <=100:
def order():  # For ascending
    for i in range(len(ADI) - 1):
        if ADI[i] - ADI[i + 1] > 0:
            return False
        return True

if True:
    vol_status_adi = "Buy"
else:
    vol_status_adi = "Sell" --- unreachable code
print("ADI signal is: ", vol_status_adi)

预期输出'''ADI 信号是:But/Sell'''

实际输出''' ADI信号为:'''

【问题讨论】:

  • 如果为真:将始终执行

标签: python for-loop pycharm unreachable-code


【解决方案1】:

True 的计算结果始终为 True。看起来您的意图可能是使用从 order 方法返回的布尔值来更改打印字符串的内容,在这种情况下您需要

if order():
    vol_status_adi = "Buy"
else:
    vol_status_adi = "Sell" --- unreachable code
print("ADI signal is: ", vol_status_adi)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-06
    • 2014-01-22
    • 1970-01-01
    • 2022-12-04
    • 2018-02-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多