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 Python Basics (2015) Number Game App String length

Sun Min
Sun Min
2,941 Points

String length

Hi guys.

I'm working on the string length challage.

strlen.py
def just_right(treehouse):

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

what i was wrong on my code?

should i use return instead of print? and i keep getting syntax errors too.. :(

1 Answer

andren
andren
28,558 Points

The challenge specifies that the values should be returned, so yes, you do have to use return instead of print. Return and print are two very different functions, even though their behavior might seem somewhat similar in the Python REPL.

The issue that causes a syntax error is that you are using the len function on "just_right", which is the name of the function, rather than "treehouse", which is the name of the variable holding the string that will be passed in.

There is a third issue, though it's not so much a coding issue as it is an issue of the code checker being very picky about text output. When you print or return a string on these challenges you often have to type the exact text the challenge is requesting, a single typo no matter how minor will often lead to your code being marked as wrong.

In this case the issue is that you have misspelled the word "too" in this string: "Your string is to short".

Fixing all of those issues leads to this code:

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

Which will pass the challenge.

Sun Min
Sun Min
2,941 Points

Thank you!