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 trialL J
438 PointsStuck on String Length Challenge
Something's wrong, but not sure where. Here's my code:
def just_right():
string = []
while len(string) < 5:
print("Your string is too short")
elif string > 5:
print("Your string is too long")
else:
return True
6 Answers
james south
Front End Web Development Techdegree Graduate 33,271 Pointsyou are close. you don't need a loop here, so change out the while for if. you don't need to use a list either so you can delete the string = [] line. your function takes a string as a parameter which goes in the first line in the parens () next to the function name. you could call it string then that is what your code would be examining to determine the length. also on your elif you need to use len(string) like with the if.
L J
438 PointsThis was my attempt to fix it:
def just_right(): if len(string) < 5: print("Your string is too short") elif len(string) > 5: print("Your string is too long") else: return True
Still getting an error: TypeError: just_right() takes 0 positional arguments but 1 was given.
I'm not exactly sure what that means. Also, thanks for the speedy reply! :)
james south
Front End Web Development Techdegree Graduate 33,271 Pointsyou need a parameter, it can be called almost anything, but you are already using the name string so go with that. it goes in the () in the first line, so def myFunction(parameter1, parameter2....) like that. when the function is called, arguments are passed in and stored in the parameter, so if you called it with donkey, in the function the variable string would hold the value donkey. then your code would evaluate how long donkey is and do whatever it is doing. your code is ok just put string in the () in the function definition - the first line where the name is.
L J
438 PointsThanks - I see what you are saying now. I've made the appropriate changes (I think), but I must be doing something incorrectly still. Here's my code again (hope you can decipher even though the formatting is off).
def just_right(string): if len(string) < 5: print("Your string is too short") elif len(string) > 5: print("Your string is too long") else: return True
Any ideas?
james south
Front End Web Development Techdegree Graduate 33,271 Pointsyou need to return the statements, not print them.
L J
438 PointsMan, I'm dumb. It worked. Thank you =D