0

I'm writing a program that takes a list of strings and return the length of of each string in a list.

def characters(nameLst):
    nameLst = ["Dan","jason","may","cole","Zhan"]
    outLst = []
    for i in range(len(nameLst)):
        outLst = outlst.append(len(nameLst))
    return (outLst) # should return [3, 5, 3, 4, 4]


nameLst = ["Dan","jason","may","cole","Zhan"]

def main():
    characters() 
main()

every time I run the program I get an error:

characters() takes exactly 1 argument (0 given)
1
  • 2
    You're writing in Python, a language with significant whitespace. Please make sure when you are pasting Python code that your indentation is correct. This time it was easy to figure out what you meant; next time, it might be complete and utter gibberish. Commented Apr 3, 2014 at 2:08

7 Answers 7

2

When you defined the method characters, you said that it takes one argument, called nameList, but when you called it inside of the main method, you don't give it any arguments, if you use

characters(nameList)

in your main method, this should fix your error.

Also, your code will not give you the lengths of the different strings in nameList, rather it will give you a list full of the length of nameList. With the given list, you would get

[5, 5, 5, 5, 5]

because the expression that you append to the list is len(nameList), when it should be len(i).

Finally, List.append() will append to the list, so you don't need to use an = sign. If you replace the line with:

outlst.append(len(nameLst[i]))

This should give you the correct output.

Edit: I just realized that you redefine nameLst inside of the characters function. It is not necessary to have nameLst both inside and outside of the function. Either define characters with no arguments and define nameLst inside of characters, or add nameLst as an argument and don't define it inside of the function.

Sign up to request clarification or add additional context in comments.

2 Comments

Its not len(i) as OP used i as an index. It should be len(nameList[i]) or better yet change the for loop to for i in nameList:.. then len(i) would work
You are correct, I changed the post to reflect that.
2

every time I run the program I get an error: characters() takes exactly 1 argument (0 given)

Here is how you invoke characters():

characters() 

Here is how it's defined:

def characters(nameLst):

Therefore, python expects you to call it like characters(names) instead of characters().

The likely fix is to move the nameLst contents to main's scope and pass it in (also your characters function does something different than you described. fix below):

 def characters(nameLst):
     outLst = []
     for name in nameLst:
         outlst.append(len(name))

     return outLst # should return [3, 5, 3, 4, 4]


 def main():
     nameLst = ["Dan","jason","may","cole","Zhan"]

     characters(nameLst) 

 if __name__ == '__main__':
     main()

Comments

1

You've written characters(nameLst) with one parameter. Hence, when calling the function, be sure to pass the list in to the method.

Additionally, you will want to return(outLst) after your for loop - this way the entire list will be returned rather than just the first item.

Comments

1

Sidenote: Here is an easy implementation for your function:

def characters(nameLst):
    return map(len, nameLst)

Example:

>>> characters(["Dan","jason","may","cole","Zhan"])
[3, 5, 3, 4, 4]

I guess, directly replacing characters(...) with map(len, ...) would be the better solution... ;-)

Comments

1

The error means exactly what it says. You declared the characters function to take a parameter called nameLst, but you called it with no parameters. Either change

def characters(nameLst):

to

def characters():

effectively making nameLst a local variable, or pass a list as a parameter when you call the function.

def characters(nameLst):
    outLst = []
    for i in range(len(nameLst)):
        outLst = outlst.append(len(nameLst))
    return (outLst)

def main():
    nameLst = ["Dan", "jason", "may", "cole", "Zhan"]
    characters(nameLst)

Furthermore, your function would be better written as a list comprehension:

def characters(nameLst):
    return [len(name) for name in nameLst]

Note that you shouldn't expect any output from your program, since you never call print().

Comments

0

Your problem is not the return, but the call. You've defined your function to take one parameter, but you haven't given it one when you call it.

Comments

0

You define a function characters, which takes a single argument nameLst, so you need to call it with an argument, like this:

def main():
    nameLst = ["Dan", "jason", "may", "cole", "Zhan"]
    result = characters(nameLst) # Save the returned list on the variable result
    print result # Print the result

main()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.