如何处理for循环中Python中无法访问的代码

How to handle unreachable code in Python in for loop

我正在尝试检查列表中的上升或下降趋势。如果趋势上升,我希望它打印 "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 signal is: But/Sell'''

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

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

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