![](/img/trans.png)
[英]Error despite Global keyword being used to access variable inside function
[英]Name is not defined error inside a function, despite using global variable, it's being ignored | Python
sendhelp()
function 之前的第一塊代碼工作正常,第二塊代碼也可以,但是當 function 執行時,它給了我一個name 'defualt_mode' is not defined
錯誤,盡管使用了global
,它完全被忽略了,因為當我刪除它時,它給了我同樣的錯誤,什么? 我想要發生的是讓它接受默認變量,但不刪除 function 並且不手動在 function 中定義它。 我所說的“手動”是什么意思:
def sendhelp():
defualt_mode = True
if defualt_mode is True:
#list of accounts
account_list = ["tech.dailys", "technologyhive", "rising.tech", "fanastic.technology", "xtechnation", "technology", "fundamentaltech", "techsector"]
另外,如果有任何替代使用 global 的方法,那就太棒了(不刪除sendhelp()
函數)
message_response = ['/V/M/itechexplore, bruh, fam, dawg', 'dksasadj', 'jdsjsalkd', 'kalsdn']
for message in message_response:
if '/V/M/' in message:
pre_account_list= []
multiple_account_scan_accounts = message.removeprefix("/V/M/")
pre_account_list.append(multiple_account_scan_accounts)
for message in pre_account_list:
if "," in message:
pre2_account_list = message.split(", ")
print(pre2_account_list)
default_mode = False
if '/V/M/R/' in message:
default_mode = True
def sendhelp():
#checks if the defualt list is active
global defualt_mode
if defualt_mode is True:
#list of accounts
account_list = ["tech.dailys", "technologyhive", "rising.tech", "fanastic.technology", "xtechnation", "technology", "fundamentaltech", "techsector"]
if default_mode is True or default_mode is False:
sendhelp()
在sendhelp()
function 的if
語句中,您正在檢查defualt_list
值是否為 True。 您剛剛聲明了變量,但尚未初始化。 如果你通過,在 if 語句之前
defualt_list = True
或者
defualt_list = False
然后它會正常工作。
我將實現如下(注意,我假設您知道 sendhelp 目前在任何一種情況下都不做任何事情。它要么定義然后丟棄 account_list,要么什么都不做,然后返回。
def sendhelp(default_mode):
if default_mode is True:
#list of accounts
account_list = ["tech.dailys", "technologyhive", "rising.tech", "fanastic.technology", "xtechnation", "technology", "fundamentaltech", "techsector"]
sendhelp(default_mode)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.