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 trial

Python

omri golan
PLUS
omri golan
Courses Plus Student 15,836 Points

python 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
Michael Hulet
47,913 Points

I 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 returned 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