NameError名称'var'未在IF语句中定义

时间:2016-05-05 06:47:19

标签: python python-3.x pandas

我有以下代码可以正常工作:

for index, row in dfcodes.iterrows():
# for each security code identify those with 3 down candles and narrowing raneg for first 3 days of 6 day set
scode=row['Security_Code']

tdate='2016-01-15'
df=fetch_last(scode,tdate,6)
dfreverse=df.sort('TradeDate', ascending=True)

#look for 3 consecutive down candles with narrowing range and then 2 up candles
dfdownbars=dfreverse.head(5)
ncnt=1

for index,row in dfdownbars.iterrows():
    otmp = row['Opening_Price']
    ctmp = row['Closing_Price']
    rtmp = abs(row['Opening_Price'] - row['Closing_Price'])
    dtmp = row['TradeDate']
    if ctmp<otmp and ncnt==1:
        o1 = otmp
        c1 = ctmp
        r1 = rtmp
        d1 = dtmp
        ncnt+=1
    elif ctmp<otmp and otmp<o1 and ctmp<c1 and rtmp<=r1 and ncnt==2:
        o2 = otmp
        c2 = ctmp
        r2 = rtmp
        d2 = dtmp
        ncnt += 1
    elif ctmp<otmp and otmp<o2 and rtmp<=r2 and ncnt==3:
        o3 = otmp
        c3 = ctmp
        r3 = rtmp
        d3 = dtmp
        ncnt += 1
    else:
        break

然而,只要我添加第4个elif,我就会收到以下错误:

elif ctmp&gt; otmp和ctmp&gt; c3和ncnt == 4: NameError:名称'c3'未定义

即。错误的代码现在看起来像这样:

for index, row in dfcodes.iterrows():
# for each security code identify those with 3 down candles and narrowing raneg for first 3 days of 6 day set
scode=row['Security_Code']

tdate='2016-01-15'
df=fetch_last(scode,tdate,6)
dfreverse=df.sort('TradeDate', ascending=True)

#look for 3 consecutive down candles with narrowing range and then 2 up candles
dfdownbars=dfreverse.head(5)
ncnt=1

for index,row in dfdownbars.iterrows():
    otmp = row['Opening_Price']
    ctmp = row['Closing_Price']
    rtmp = abs(row['Opening_Price'] - row['Closing_Price'])
    dtmp = row['TradeDate']
    if ctmp<otmp and ncnt==1:
        o1 = otmp
        c1 = ctmp
        r1 = rtmp
        d1 = dtmp
        ncnt+=1
    elif ctmp<otmp and otmp<o1 and ctmp<c1 and rtmp<=r1 and ncnt==2:
        o2 = otmp
        c2 = ctmp
        r2 = rtmp
        d2 = dtmp
        ncnt += 1
    elif ctmp<otmp and otmp<o2 and rtmp<=r2 and ncnt==3:
        o3 = otmp
        c3 = ctmp
        r3 = rtmp
        d3 = dtmp
        ncnt += 1
    elif ctmp > otmp and ctmp > c3 and ncnt==4:  # first up candle after 3 downs
        o4 = otmp
        c4 = ctmp
        r4 = rtmp
        ncnt += 1
    else:
        break

cnt -= 1

有人告诉我为什么在我添加最终'elif'之前所有变量c1-c3被识别时,在最后的'elif'语句中没有识别变量'c3'? 顺便说一下,我正在迭代一个pandas数据帧

非常感谢任何帮助

格伦

1 个答案:

答案 0 :(得分:0)

TL; DR:如果你先检查ncnt,你可能会工作:

`if ncnt==4 and ctmp > otmp and ctmp > c3:`

对于更长的答案,你必须首先接受你正在做一些有点棘手和不寻常的事情。您正在else子句中定义名称,并相信您的程序将运行这些cluses来定义名称,然后再在代码中检查它们。

也就是说,在你的for循环的第一次迭代中,你点击了这一行:

  if ctmp<otmp and ncnt==1:

因为您运行代码来定义新变量o1c1r1是正确的。如果第一次迭代的条件为假,那么接下来你会点击这一行:

  elif ctmp<otmp and otmp<o1 and ctmp<c1 and rtmp<=r1 and ncnt==2:
    o2 = otmp

Python会开始查找变量,尝试获取o1的值并立即给你NameError: name 'o1' is not defined例外。

有两个真正的选择:

  1. 在使用它们之前定义所有名称,可能将它们设置为0.这是首选方法,避免让那些负责维护代码年限的人员跟踪。

  2. 首先仔细检查您的州。此代码是一个小型状态机,其当前状态由ncnt指出。如果您的代码首先检查它是否处于正确状态,那么您将永远不会引用错误变量。因此,更好的代码将重命名ncount并使用if nState == 3 and o3 > something...启动if语句。一旦到了 nState是错误的,if语句将在不查看尚不存在的变量的情况下停止。

  3. 继续编码。