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

Yessica Schoonen
Yessica Schoonen
1,553 Points

Task 1 TypeError

I keep getting typeErrors.. I'm having a hard time grasping remembering what I've learnt from the videos. Someone any tips for me?

strlen.py
def just_right():
    string = []
    while len (string) < [5]:
        try:
            length = int(input("You string is too short"))
        except ValueError:
            print("Your string is too long")

            string.append(length)

    just_right()      

1 Answer

Lets go through the question and we'll solve the task and learn while doing that.

create a new function

def just_right():

the function takes a single argument

def just_right(string_argument):

if length of argument is less than five, return...

    if len(string_argument) < 5:
        return "Your string is too short"

if length is more than 5 return ...

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

other wise return true

    return True
#OR since the program-control will come out of if&elif, in case the length of string is 5 so doesn't matter if we return through else or through the program, Right?
    else:
        return True

Final Solution(remember the indentation)

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

OR

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

Now lets call the function: just_right("123456")

And hard time while learning and remembering??? it quite common, don't stress yourself. give it time :) You already are smart that you opted for Python, i started with C/C++ and i couldn't understand a thing for a year.

!Dont tell that to my manager =D. Enjoy coding.

Hope this helps!