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

Munesh Raj Lakhey
Munesh Raj Lakhey
4,408 Points

What's wrong with this fucntion to check length of a string?

Is something wrong with syntax?

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

2 Answers

Tobias Helmrich
Tobias Helmrich
31,602 Points

Hey Munesh,

no, the syntax is correct and you almost got it working, good job! The only problem is that you have to return the strings but you're printing them out.

This should work:

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

I hope that helps! :)

Munesh Raj Lakhey
Munesh Raj Lakhey
4,408 Points

Thanks again. Missing small details!