Have an odd issue while using the active_directory module, which is amazing BTW… awesome wrapper.
Anyhow, I have a need to make sure that a username is unique… heres my function:
import active_directory
def creative_name(fname, lname, n=1):
uname = fname[:n] + lname
x = active_directory.find_user(uname)
if x: creative_name(fname, lname, n+1)
else: return uname
if __name__=='__main__':
print creative_name("Sarah", "Smith")
it keeps coming up with None… I would expect it to return uname… or
fname[:n+1] + lname
its strange because if I run the function like this:
def creative_name(fname, lname, n=1):
uname = fname[:n] + lname
x = active_directory.find_user(uname)
if x: creative_name(fname, lname, n+1)
else: print uname
>>>
SaSmith
>>>
SaSmith is returned… why won’t it return the value?!?! print uname works but return uname returns None!
Please help!
Thanks all,
Randy
It wasn’t returning
SaSmithbecause the recursive part wasn’t being returned, so ifxwas true the function returnedNone. Adding in onereturnshould fix:.
Note: the first line prevents an infinite loop if
active_directory.find_user(fname[:n]_lname)is True for everyn.I agree with @Randall that using a
forloop is probably more readable, it returnsNonein the above case.