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

in challenge string lengths I am getting a bummer although the code is correct

def just_right(string): string = str() if len(string) < 5: print("Your string is too short")

elif len(string) > 5:
    print("Your string is too long")

else:
    return True

3 Answers

I solved it I solved it thanks a lot cuz it was not just the return but the assignment of the argument was inside the function but it was supposed to be outside.

can you let me know why?

Are u talking about the code you posted"string = str()"? I don't know why u need"str()". Str() is a function to change a int or variable into a string.

#correct answer
#i use a variable "str" for asingle argument in function just_right. i know you use argument"string" ,but they are same. You can name it whatever you want.
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
#What is string=str()? i see this in your code. i think this is not how we change var or int into a string.


#i still don't know what is "string = str() ".
## This is how we use convert to string statment with str() function
#if you type str(3) in python shell,it will convert int 3 into a string "3".
#if you type str(var_apple) in python shell ,it will covert var_apple into a string "var_apple".

string = str() should'n be there. It looks like it assigns the variable string to an empty string.

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Although you haven't linked the challenge here, I think I remember it. I feel fairly certain that the challenge asks you to return the strings, not print them. If this turns out to not be the case, could you please link the challenge? :sparkles:

This is the case but still not working, here is the challenge link:

https://teamtreehouse.com/library/python-basics/number-game-app/string-length

python'''

def just_right(string): string = str() if len(string) < 5: return "Your string is too short"

elif len(string) > 5:
    return "Your string is too long"

else:
    return True