[Tutor] TypeError: generatePersonID() takes exactly 1 argument (0 given)
Alan Gauld
alan.gauld at btinternet.com
Thu Nov 14 18:32:13 CET 2013
On 14/11/13 15:54, Thabile Rampa wrote:
> Hi,
>
> So I'm learning how to define my own functions, and in an exercise I was
> given, I get this error:
>
> Traceback (most recent call last):
> <http://function_practice.py/function_practice.py>", line 34, in main
> generatePersonID ()
> TypeError: generatePersonID() takes exactly 1 argument (0 given)
OK, The error says you haven't passed an argum ent to your function.
Lets take a look at the function...
> def generatePersonID (fullName):
> """generates unique ID"""
> global id
> id = (fullName) + 1
> personID = str (id) + fullName
> return personID
Yep, it needs an input argument. And it returns an ID.
But it looks a bit odd so I'll walk through it and see if
what I think its doing matches up with what you expect
it to do.
> def generatePersonID (fullName):
This defines the function so that you can call it like
myID = generatePersonID(someID)
> """generates unique ID"""
> global id
This says it uses a global variable called id. I don't see any such
variable in your code. It's usually a bad idea to rely on global
variables like this. Either pass the variable in as an extra argument or
avoid using it if possible. Only use globals as a last resort.
> id = (fullName) + 1
This is where the wierdness happens. You add 1 to the argument
passed in which is called fullName. Now either fullName is actually
a number or you don't want to add 1 to it. If it is a number
then change the name of the parameter. If it is really a string
then maybe you want to append the character '1' to it? If so
just use '1' instead of 1. Either way, you don't need the
parens around fullName, they don't do anything.
> personID = str (id) + fullName
More wierdness. Here you take the new id value created above
and convert it to a string then add the fullName again. But
if fullName is a number this will fail. And if its a string
and you meant to append'1' above then you get the odd result
(if fullname is "alan gauld") of personID ending up like
"alan gauld1alan gauld"
Which is not what I think you want?
> return personID
And finally you return the result, no problems there.
So I think you need to think through what types of data you
are passing in and how you manipulate them. There is
definitely something wrong as it stands.
HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list