Python UnboundLocalError:局部变量';交易id';分配前参考

Python UnboundLocalError:局部变量';交易id';分配前参考,python,Python,我需要一些帮助来识别这个错误。 Python对我来说是新事物,所以我每天都在努力学习 class WithdrawHandler(BaseHandler): def get(self): self.redirect(u"/") @gen.coroutine def post(self): if not self.current_user: self.set_status(403, "Forbidden. Please log in first.")

我需要一些帮助来识别这个错误。 Python对我来说是新事物,所以我每天都在努力学习

class WithdrawHandler(BaseHandler):
def get(self):
    self.redirect(u"/")

@gen.coroutine
def post(self):
    if not self.current_user:
        self.set_status(403, "Forbidden. Please log in first.")
        return

    withdraw_address = json.loads(self.get_argument("withdrawAddress", None))
    username = tornado.escape.json_decode(self.current_user)
    withdraw_amount_str = json.loads(self.get_argument("withdrawAmount", None))
    withdraw_amount = None
    try:
        withdraw_amount = float(withdraw_amount_str)
    except Exception:
        logging.exception("Withdraw Address: " + withdraw_address + " | Username: " + username + " | Withdraw Amount: " + withdraw_amount_str)
        self.set_status(400, "Invalid amount to withdraw.")
        self.finish()
        return

    if not self.coindaemon.validateaddress(withdraw_address).isvalid:
        logging.info("[Invalid Withdraw Address] Withdraw Address: " + withdraw_address + " | Username: " + username + " | Withdraw Amount: " + withdraw_amount_str)
        self.set_status(400, "Invalid withdraw address. Please enter a valid value.")
        self.finish()
        return        

    if float(self.coindaemon.getbalance(username, minconf=2)) < withdraw_amount:
        logging.info("[Insufficient Funds] Withdraw Address: " + withdraw_address + " | Username: " + username + " | Withdraw Amount: " + withdraw_amount_str)
        self.set_status(400, "Insufficient funds to withdraw.")
        self.finish()
        return

    transaction_time = get_sql_datetime()

    try:
        try:
            self.coindaemon.walletpassphrase(options.walletpassword, 60)
        except bitcoinrpc.exceptions.WalletAlreadyUnlocked:
            pass
        transaction_id = self.coindaemon.sendfrom(username, withdraw_address, withdraw_amount, minconf=2, comment="Withdraw")
        query = 'insert into transactions (transaction_id, transaction_type, transaction_time, username, amount, withdraw_address) values (%s, %s, %s, %s, %s, %s);'
        addtransaction = yield momoko.Op(self.db.execute, query, (transaction_id, "withdraw", transaction_time, username, withdraw_amount, withdraw_address))
        balance = self.coindaemon.getbalance(username, minconf=2)
        self.write(dict(bal=str(balance)))
        self.set_status(200)
    except Exception:
        logging.exception("Transaction ID: " + transaction_id + " | Username: " + username + " | Withdraw Address: " + withdraw_address + " | Withdraw Amount: " + withdraw_amount_str + " | Balance: " + str(balance))
        self.set_status(400, "Error withdrawing.")    
    finally:
        self.finish()

希望有一些大师能帮我带路

如果
self.coindaemon.sendfrom
引发异常,则不会设置
transaction\u id
,因此无法将其记录在except子句中。您可以尝试将变量设置为该行之前的默认值。

看起来

self.coindaemon.walletpassphrase(options.walletpassword, 60)

引发的错误不是
bitcoinrpc.exceptions.WalletAlreadyUnlocked
,因此内部
try
语句无法捕获它,而外部
try
语句捕获它。因此,
transaction\u id
在被用于
异常除外
子句之前从未设置过。

self.coindaemon.sendfrom(用户名、取款地址、取款金额、minconf=2、comment=“取款”)返回什么?一般性意见:尽可能捕获特定的异常如果可以的话,这在实践方面不是很好
除了异常:
。我们将对此做一些详细介绍,感谢您指出,您也不应该捕获赤裸裸的异常。只捕获您期望发生的特定异常。谢谢。无法从外部配置文件中正确读取的是options.walletpassword。这导致您提到的行抛出错误。
self.coindaemon.walletpassphrase(options.walletpassword, 60)