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

Sahil Sharma
Sahil Sharma
4,791 Points

How to ensure a string as an argument for a function?

This code returns a NameError : 'a' is not defined. How can I ensure that a is a string and declare it?

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

1 Answer

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

You're doing fine! And in this challenge it's not necessary to ensure that what's being sent in is a string. That's taken care of by Treehouse. They've guaranteed that they're sending in a string. But you're correct, checking for that is a great idea! We're just not really there yet.

What you've missed is the line in the instructions where it says to return the string... not print it. So if you remove this line here:

a = str(a)

and then change all your print statements to return statements, your code passes! Happy coding! :smiley: