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 trialChristian Coker
3,872 PointsWhy is this code wrong?
This code should work but I keep getting it wrong. Does anyone know why?
def just_right(mystring):
if len((list(mystring))) > 5:
print("Your string is too long")
elif len((list(mystring))) < 5:
print("Your string is too short")
else:
return True
2 Answers
Anish Walawalkar
8,534 PointsHey Cristian, there are only 2 same problems in your code:
- In python, strings are an array of characters so you can just do len(mystring)
- If the the string is too long/too short you need to return the message not print
In code it looks like this:
def just_right(mystring):
if len(mystring) > 5:
return "Your string is too long"
elif len(mystring) < 5:
return "Your string is too short"
else:
return True
you could also make you5 code look cleaner if you implement it like this:
def just_right(mystring):
if len(mystring) == 5:
return True
return "Your string is too short" if len(mystring) < 5 else "Your string is too short"
Christian Coker
3,872 PointsThanks a lot Anish, that worked!