Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics (2015) Number Game App String length

I get Try again, the same piece of code worked in a separate python console

def just_right(word): if len(word) < 5: print ("Your string is too short") elif len(word) > 5: print ("Your string is too long") else: return True

strlen.py
def just_right(word):
    if len(word) < 5:
        print ("Your string is too short")
    elif len(word) > 5:
        print ("Your string is too long")
    else:
        return True

3 Answers

andren
andren
28,538 Points

The challenge asks you to return the strings, it does not ask you to print them. While those two things looks somewhat similar when you run the code in the REPL they do very different things, as you'll start to learn later on.

So if you replace the print calls with return statements like this:

def just_right(word):
    if len(word) < 5:
        return "Your string is too short"
    elif len(word) > 5:
        return "Your string is too long"
    else:
        return True

Then your code will be accepted.

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,704 Points

Hi there,

Yes, your code will work in the Python Console, because it is correct syntactically :thumbsup:. However, it is not exactly what the challenge asks for. Challenges are very specific with the instructions and it needs to be followed explicitly. Here the instructions say to "return" the strings, but you are printing them.
Just fix that up and you're good to go! :)

Keep Coding! :dizzy:

Thanks, guys! That was my bad.