Note that there are some explanatory texts on larger screens.

plurals
  1. POReturning function variable error ~local variable 'user' referenced before assignment~, Python
    primarykey
    data
    text
    <p>I've written a function and I wanted to return a variable from that function, but I keep getting an error</p> <p><em>local variable 'user' referenced before assignment</em></p> <p>My function is:</p> <pre><code> 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 </code></pre> <p>Further down my code, I call the function:</p> <pre><code>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 </code></pre> <p>I set some print messages to print 'user', but it doesn't seem to be returning anything. Before I created the function I had the code within it, hardcoded below the 'for loop' and it worked....so I'm stumped as to why it's not returning any values.</p> <p>Any suggestions?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload