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

don't know what is wrong my answer of Python Basic Challenge Question

Help me out please.

I've got "Try again!" message but I don't know what is WRONG my answer.

Here's 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.

Here's my answer

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

2 Answers

You are almost right but you forgot to put a colon after you if statement and you have to use a less than operator to check is less than 5 characters, and you have to return the string instead of printing it.

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

I hope that helps a little bit

Hi Carlos, Can you and/or anyone please tell me why I can't use 'print' instead of 'return?' For example:

print("Your string is too short") print("Your string is too long")

instead of:

return "Your string is too short" return "Your string is too long"

If yon use print() then it just display the result on your screen. But if you return the result then you can print out side of the function or assaign to other variable/s

And the question asks "return" not print or display on your screen.

That's why you and I got thr "try again" message. :)

Ok, thanks Dong.