Welcome to the Treehouse Community
Looking to learn something new?
Usmaan Amin654 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...
#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")
james southFront End Web Development Techdegree Graduate 33,258 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 Parker215,954 Points
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.