gpt4 book ai didi

python - 未绑定(bind)本地错误: local variable 'transaction_id' referenced before assignment

转载 作者:行者123 更新时间:2023-12-01 05:03:17 26 4
gpt4 key购买 nike

我需要一些帮助来识别此错误。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()

当它触发时,它会给我错误。我一直在搜索并找到了一些关于设置 transaction_id 全局的信息。这让我很困惑。

Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/tornado/web.py", line 1221, in _when_complete
if result.result() is not None:
File "/usr/local/lib/python2.7/dist-packages/tornado/concurrent.py", line 129, in result
raise_exc_info(self.__exc_info)
File "/usr/local/lib/python2.7/dist-packages/tornado/gen.py", line 227, in wrapper
runner.run()
File "/usr/local/lib/python2.7/dist-packages/tornado/gen.py", line 531, in run
yielded = self.gen.send(next)
File "main.py", line 282, in post
logging.exception("Transaction ID: " + transaction_id + " | Username: " + username + " | Withdraw Address: " + withdraw_address + " | Withdraw Amount: " + withdraw_amount_str + " | Balance: " + str(balance))
UnboundLocalError: local variable 'transaction_id' referenced before assignment

希望那里的一些大师可以帮助我带路!

最佳答案

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

关于python - 未绑定(bind)本地错误: local variable 'transaction_id' referenced before assignment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25593940/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com