As @JacobSmall says, it’s useful to know what you are plugging in as it’s hard to say what the issue is.
However, you could help yourself by putting the try catch inside the for loop instead of outside. This way it is easier to debug where you are having the error as it might only be one element failing but the whole thing fails as it is currently written.
Another thing is you that should have your transaction open and close statements in-line. Currently, if the code within the Try scope fails, you skip closing the transaction and go straight to Except.
Also, Element.Pinned is a property so you can set it with a simple Element.Pinned = True/False.
Here is some working code which is a little more concise and should give you better insight to where you are getting errors.
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument
from Autodesk.Revit.DB import *
# Ensure input is always a list...
if hasattr(obj1,"__iter__"): return obj1
else: return [obj1]
# IN Variables...
elems = tolist(UnwrapElement(IN))
b = tolist(IN)
# OUT Variables...
outList = 
# Main Body...
for e in elems:
e.Pinned = b
except Exception, ex:
OUT = outList
Hope this helps.