sys.exit()之后的UnboundLocalError

时间:2017-04-12 08:26:51

标签: python-2.7

这是我的错误:

Traceback (most recent call last):
  File "./datalogger.py", line 208, in <module>
    calculateamounttosend()
  File "./datalogger.py", line 36, in calculateamounttosend
    return send_amount
UnboundLocalError: local variable 'send_amount' referenced before assignment

第208行是:

calculateamounttosend()

功能是:

def calculateamounttosend():
        wallet_balance = float(subprocess.check_output(['solarcoind', 'getbalance'], shell=False))
        if wallet_balance < 0.0005:
                print ("*******ERROR: wallet balance of {}SLR too low for reliable datalogging, add more SLR to wallet $
                time.sleep(10)
                sys.exit
        elif wallet_balance >= 10:
                send_amount = str(1)
                print ('Based on wallet balance of {} amount to send to self set to {} SLR') .format(wallet_balance, se$
        elif wallet_balance < 10 and wallet_balance >= 0.03:
                send_amount = str(0.01)
                print ('Based on wallet balance of {} amount to send to self set to {} SLR') .format(wallet_balance, se$
        else:
                send_amount = str(0.00001)
                print ("*******WARNING: low wallet balance of {}SLR, send amount of {} may result in higher TX fees****$
        return send_amount

当wallet_balance低于0.0005时,它会执行sys.exit(),但之后并没有真正停止,它似乎会读取其余代码并给出错误。我不清楚为什么我得到错误,如果程序退出,如果我有回报,为什么它导致问题无论如何。

1 个答案:

答案 0 :(得分:1)

您必须调用此功能。 将sys.exit替换为sys.exit()

P.S。但使用这种特殊类型的应用程序关闭真的很糟糕。尝试使用return或smth之类的异常(这也是非常糟糕的主意,但在处理过程中比sys.exit()更好,你可以打印调试消息)。