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

What is being asked for in the just right method?

This is the question

Create a new function named just_right that takes a single argument, a string.

If the length of the string is less than five characters, return "Your string is too short".

If the string is longer than five characters, return "Your string is too long".

Otherwise, just return True.

My code is below. I have tested my code and it seems to forfil the objectives. What does Bummer! Try again! actually mean?

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

1 Answer

andren
andren
28,558 Points

It means that your code did not match the criteria the challenge checker was looking for. The issue with your code is that it wants you to return a string if the length is too short or too long, not print it. Printing and returning are two very different things.

If you change your print statements to return statements like this:

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

Then you'll be able to pass the challenge.

What the difference between return and print is might not seem super obvious at this point, and it is pretty common for beginners to confuse the two of them since they behave similarly in certain simple exercises. But when you get farther into learning Python and start to use functions in more advanced ways you'll start to understand what return is actually doing, and will start to understand why it's quite different from simply printing a value.

Thanks andren. I went back to it just now as it was the last thing outstanding on the Python course and noticed it said return and not print.

Up until this point in practically all challenges where a string is involved it asks for a string to be printed and my brain became blind to the word "return"

andren
andren
28,558 Points

Ah I see that makes sense. I assumed you were a beginner who thought print and return did the same thing, since that is actually a pretty common topic of confusion among new Python programmers on this forum. But yes if you are just skimming though these challenges to get them marked as completed, then I certainly understand why you might make a simple mistake like that.