Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics (2015) Number Game App String length

Maggie Wolff
Maggie Wolff
495 Points

python basics challenge task 1 of 1 "string length"

I can't figure out how to even start it, could some one reword the question, or give me a clue on how to do this?

strlen.py

3 Answers

Josh Keenan
Josh Keenan
19,529 Points

Hey Maggie, create a function that takes an argument, which is a string.

Then you need to check how long the string is.

If it is longer than 5 characters do x

If it is shorter than 5 characters do y

Anything else, just return True.

If you need any more help just let me know!

Maggie Wolff
Maggie Wolff
495 Points

ok so, I do have another question...

def just_right:
    length = input("Enter a string: ")
    if length > 5:
        print("Your string is too long")
    elif length < 5:
        print("Your string is too short")
    else:
        return True

I can't figure out what's wrong with this.

Greg Kaleka
Greg Kaleka
39,019 Points

Hi Maggie - don't print; return!

Other than that, your code looks right. I made some formatting fixes. You should use backtics (`), not apostrophes (') to surround your code. You also need to put them on a new line before and after your code. Lastly, you can add the language you're using to get syntax highlighting. Here's what it looks like:

```python

[your code goes in here]

```

Josh Keenan
Josh Keenan
19,529 Points
def just_right(argument):       # creating the function with an argument, 
                                # this argument is replaced with the string that the test will pass in
    if len(argument) < 5:       # IF the length of the string is less that 5 characters DO:
        return "Your string is too short"

    elif len(argument) > 5:     # ELSE IF the length of the string is greater than 5 characters DO:
        return "Your string is too long"

    else:
        return True             # If it is anything else (5 exactly), return True

Here's my solution, I explain each line, feel free to ask me any questions that you might have about it!

Maggie Wolff
Maggie Wolff
495 Points

ok, so i don't really know what you, or the question means by 'argument'

Josh Keenan
Josh Keenan
19,529 Points

An argument is just the value you pass into the parentheses (), now when you create a function it doesn't run, it can be run by calling it but you are just creating the outline for it.

You are telling the function to expect a string, this string can be anything and argument is just a name for it in this instance but you could use anything for it. The name is simply symbolic, so in this case the argument is actually called argument.

Does that help?