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

Carlos Pinzon
Carlos Pinzon
570 Points

String Length Quiz, keeps saying Try Again with an OK code

Hey guys,

I did the String Length code quiz, but the platform keeps on saying "try again". I checked the code in workspace and its all good.

My only guess is that I didnt need to declare the variable at the beginning, but when I dont there is an error that inside the function, the variable "name" is not defined. So I dont understand how does the platform want the answer to be given.

Thanks in advance!

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

2 Answers

Ryan S
Ryan S
27,276 Points

Hi Carlos,

You are correct that you shouldn't declare the "name" variable before the function. So when you delete it and receive the error "name not defined", it is because you are calling the function at the end of your code and trying to pass in a variable called "name" that does not exist anymore.

You don't need to call the function yourself in the challenge. The code checker will call it on its own and pass in whatever values it wants to.

But the reason your code isn't passing (even after deleting the extra statements outside your function definition), is because you are asked to return the strings, not print them.

It is often convenient to print things when you are testing your code in workspaces, but keep in mind that there is a big difference between the two.

Hope this helps.

Carlos Pinzon
Carlos Pinzon
570 Points

Thank you so much ! That was very enlighting and helpful I will try it that way thanks !

I thought tha "return" would give me something back but since my workspace didnt give me anything I guessed I was wrong and added a return.

Thanks !!

Your code is valid, but the question asks for a 'return' not print(), here:

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

Cheers.

Carlos Pinzon
Carlos Pinzon
570 Points

Thank you for taking the time to edit this, you provided me with another part of the answer that I wasnt thinking of like the last 2 lines.