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

Jonathan Camacho
Jonathan Camacho
1,504 Points

It says that i cant turn an int object into a string implicitly, but I think it's quite fine. I dont understand.

Please help me out

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

1 Answer

Rich Zimmerman
Rich Zimmerman
24,063 Points

The index method will return the index that a given argument is at. like

string = "string"
string.index("s")

will return 0 because "s" is at index 0

You want to test the length of the string with the len() method.

string = "string"
len(string)

will return 6 because "string" is 6 characters long. So you want to use the len method in your conditional statements.

Jonathan Camacho
Jonathan Camacho
1,504 Points

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

Thank you for that, however, the len function still did not fix the error. It says "Bummer, try again!"

Rich Zimmerman
Rich Zimmerman
24,063 Points

you want

return "Your string is too short"
#and
return "Your string is too long"

not print()