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

Usmaan Amin
Usmaan Amin
654 Points

When creating a function what does "takes 0 positional arguments but 1 was given" mean?

I get the error above thrown up when I try and run the code and I can't seem to fix it. It looks like it works fine in workspaces...

strlen.py
#create a function
def just_right():

    #create single argument which takes a string
    guess = input(str("Type in a sentence: "))

    print(guess)

    #if the length of the string is less then 5, print output
    while len(guess) < 5:

        print("Your string is too short")
        break

        #if the length of the string is more than 5, print output
        if len(guess) > 5:
            print("Your string is too long!")
            break

        #otherwise return True
        else:
            print("True")

2 Answers

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

the challenge asks you to write a function that takes an argument, not one that asks for user input. you don't need any loops either, just if/elif/else logic. when defining a function, after we put the def keyword and the name, we put parens (). yours are empty, which means your function takes 0 arguments. to make it take 1 argument, put a parameter name in the parens, like (string). the parameter is the internal variable that holds the value of the argument given when the function is called. so in the function body, after calling the function with an argument like 'cat', the parameter string will hold the value 'cat' and thus string, your parameter name, will be determined to be longer than 5, shorter than 5, or 5 letters long.

Steven Parker
Steven Parker
229,005 Points

:warning: Be careful about testing challenges in the workspaces.

If you have misinterpreted the instructions, you may be also be likely to misinterpret the results.

In this case the instructions said to create a function "that takes a single argument", but your function does not take any argument. So when the challenge tried to pass it one that error was generated.

Your code seems to be using an input but it should use the passed argument instead. Also note that your function does not currently return anything. But at several points you print something but there are no instructions about printing anything. And the function only needs to respond to the argument once, so no loop will be needed.