Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialomri golan
Courses Plus Student 15,836 Pointspython function returns a none value for no reason
def getUsername():
username = raw_input("Enter Your Username: ")
if not username[0].isalpha():
print "wrong"
getUsername()
else:
return username
this function return None
if i run it more then once can anybody tell me why ?
1 Answer
Michael Hulet
47,913 PointsI haven't explicitly tested, but it's likely because this is intended to be a recursive function, but you don't return
the call when it's called from within itself. Instead, when called from inside itself, the innermost return
value is discarded, and None
is passed up and return
ed by the original invocation. You can fix it like this:
def getUsername():
username = raw_input("Enter Your Username: ")
if not username[0].isalpha():
print "wrong"
# You need to have a return statement here
return getUsername()
else:
return username