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

jamie Macdiarmid
jamie Macdiarmid
2,048 Points

Stuck

I haven't been coding in the last month and so thought I'd just put down what I remembered, Am I way off? Do I need an 'in' or to use an 'index'

Thanks

Jamie

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

4 Answers

Paul Cox
Paul Cox
12,671 Points

It's nearly right.

1) You're missing syntax on the first if (the ending colon).

if len(str) < 5:

2) You should be returning a value rather than printing it

return "Your string is too short"

3) You should include the condition directly in the elif statement

elif len(str)> 5:
jamie Macdiarmid
jamie Macdiarmid
2,048 Points

Oh. Thanks Paul. I appreciate it

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hey Jamie,

You're not "way off," but you do have a few errors.

First, you're missing a colon after the first if statement to open the if block. Your elif resembles JavaScript syntax, which Python doesn't like. :) (and took me a bit to catch too). The colon needs to be place after the condition, and I'm not sure why you have another if statement inside there. Lastly, it's just an error in what the challenge asks for. It wants you to return the strings, and you are printing them.

Have a look at the code below. I'm sure it will all come back quite quickly. :)

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

:dizzy:

jamie Macdiarmid
jamie Macdiarmid
2,048 Points

Thanks Jason. Glad I remembered some it.

Revised the code a bit. Things to look out for: make sure your if statements end with ':' and an indention on the next line elif(else if) is it's own statement so no need to have an if statement inside of it also. When it tells you to 'return' something, then use the command 'return' don't print it out.

def just_right(str):
    if len(str) <  5:
        return "Your string is too short"
    elif len(str) > 5:
        return "Your string is too long"
    else:
        return True
Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi jamie,

you are close. I see missing colons (:) after both if declarations. And donΒ΄t print the statements return them. After you got the conditions right return True.

Let me know if you need a code spoiler :)

The len method is perfect ...

Grigorij