我寫了一個功能,我想從該函數返回一個變量,但我一直收到錯誤返回函數變量錯誤〜局部變量「用戶」分配〜之前引用的Python
局部變量「用戶分配之前引用
我的功能是:
def txtelm():
# Updates date field to the current date
if textElement.name == "DATE":
textElement.text = strftime("%y %m %d")
if textElement.name == "CHECK":
textElement.text = "AB"
# First code block replaces the "Drawn" title block initials
if docauthor == "Mike" and textElement.name == "DRAWN":
textElement.text = "MM"
user = "mm"
#print user
elif docauthor == "Amy" and textElement.name == "DRAWN":
textElement.text = "AB"
user = "ab"
#print user
elif docauthor == "Ian" and textElement.name == "DRAWN":
textElement.text = "IB"
user = "ib"
elif docauthor == "Chris" and textElement.name == "DRAWN":
textElement.text = "CM"
user = "cm"
elif docauthor == "Cynthia" and textElement.name == "DRAWN":
textElement.text = "CC"
user = "cc"
return user
再往下我的代碼,我調用該函數:
for textElement in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
txtelm()
user = txtelm()
if textElement.name == "PATH":
textElement.text = outloc + "\\" + sitename.replace(" ", "_") + "_" + trunc + strftime("%d%b%y") + "_" + user
我設置了一些打印消息來打印'用戶',但它似乎沒有返回任何東西。在我創建函數之前,我已經有了代碼,在'for循環'之下進行了硬編碼,並且它的工作原理....所以我很難理解它爲什麼沒有返回任何值。
有什麼建議嗎?
稍微容易閱讀文本格式:'textElement.text = os.path.join(outloc,「%s_%s%s_%s」%(sitename.replace(「」,「_」),trunc,strftime (「%d%b%y」),user))' – 2012-03-22 15:10:27
謝謝你的建議傑森。 – Mike 2012-03-22 16:24:43