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

Paul Heneghan
Paul Heneghan
14,380 Points

I've tried multiple variations on this with print or return, with and without parentheses.

def just_right(myStr): if myStr.length < 5: return "Your string is too short" elif myStr.length > 5: return "Your string is too long" elif myStr.length == 5: return True

strlen.py
def just_right(myStr):
    if myStr.length < 5:
        return "Your string is too short"
    elif myStr.length > 5:
        return "Your string is too long"
    elif myStr.length == 5:
        return True        

3 Answers

It's supposed to be len(myStr), not myStr.length.

Gavin Ralston
Gavin Ralston
28,770 Points

len() is a function you can use on collections, not a method you call from collections. Kevin's got it right.

Paul Heneghan
Paul Heneghan
14,380 Points

D'oh! My apologies for the javascript "bleedover", that makes perfect sense, and --- it works! Thanks Kevin, and thank you Gavin for the fine tuning addition.

I tried this code and i am getting "Bommer" why?

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

UPDATE: it should be "returned" not printed, therefore correct code is:

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