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

Peter Pop
Peter Pop
3,363 Points

What is wrong with my code?

I do not understand what is wrong with my code??

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

3 Answers

Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points

It's a small typo.

You wrote: elif len(a) > 5: return "Your string is too short" // change this line to // return "Your string is too long"

Alex Watts
Alex Watts
8,396 Points

Hello Peter,

Your code is not going to run because you have not called/invoked the function just_right. Also, you have not set a value for the argument a. This argument should contain an iterable like a string or a list. You cannot pass an integer or a float as they cannot be looped through. Furthermore, you should replace the return keywords for print as return will not show the strings.

def just_right(a):
    if len(a) < 5:
        print("Your string is too short.")
    elif len(a) > 5:
        print("Your string is too big.")
    else:
        print("Your string is just right.")
just_right("Hello") #This code calls the function and assigns the word 'Hello' to 'a'.

Hope this helps :)

Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points

Did you check out the challenge?

He is supposed to do it like this:

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