Python Forum

Full Version: TypeError: get_formatted_name() missing 1 required positional argument: 'last_name'
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This is my code...

def get_formatted_name(first_name, middle_name, last_name):
    """Return a full name, neatly formatted."""
    if middle_name:
        full_name = first_name + ' ' + middle_name + ' ' + last_name
    else:
        full_name = first_name + ' ' + last_name
    return full_name.title()

musician = get_formatted_name('john', 'lee', 'hooker')
print(musician)

musician = get_formatted_name('jimi', 'hendrix')
print(musician)
This is what I get...

Error:
John Lee Hooker Traceback (most recent call last): File "C:/Python Tutorials/formatted_name.py", line 12, in <module> musician = get_formatted_name('jimi', 'hendrix') TypeError: get_formatted_name() missing 1 required positional argument: 'last_name'
As you can see it works for John Lee Hooker. This is because it has first, middle and last names. The second Jimi Hendrix, doesn't work (I think) because it's missing a middle name.

As always any help is most appreciated!!
You are getting an error in the second call (jimi Hendrix)
because
get_formatted_name(first_name, middle_name, last_name)
requires 3 arguments, as presented, there are only 2, so this function sees
jimi as first name amd Hendrix as middle name.
You can define the function with variables, like:
def get_formatted_name(first_name=None, middle_name=None, last_name=None):
and call with:
musician = get_formatted_name(first_name='john', middle_name='lee', last_name='hooker')
musician = get_formatted_name(first_name='jimi', last_name='hendrix')
that corrects the argument problem, but now you have to check for None in all fields:
def get_formatted_name(first_name=None, middle_name=None, last_name=None):
    full_name = None
    if first_name is not None:
        first_name = first_name
    if middle_name is not None:
        full_name = full_name + ' ' + middle_name
    if last_name is not None:
        full_name = full_name + ' ' + last_name
    return full_name
Thank you! That is exactly what I was looking for!
(Mar-27-2018, 09:34 PM)Larz60+ Wrote: [ -> ]that corrects the argument problem, but now you have to check for None in all fields:
def get_formatted_name(first_name=None, middle_name=None, last_name=None):
    full_name = None
    if first_name is not None:
        first_name = first_name
    if middle_name is not None:
        full_name = full_name + ' ' + middle_name
    if last_name is not None:
        full_name = full_name + ' ' + last_name
    return full_name

Which can be avoided with something like:

def get_formatted_name(*args):
    full_name = ' '.join(args)
    return full_name
@hbknjr - this is not so good solution as it (i) does not gurantee the order in which arguments were supplied (ii) the number of arguments, e.g.
get_formated_name('Hooker', 'Lee', 'John')
get_formated_name('Hooker', 'Lee', 'John', Jimi', 'Hendix')
you need to put efforts when parsing args

a similar, but in my opinion better solution is to force using keyword arguments (only python3)
def full_name(*, first_name=None, middle_name=None, last_name=None):
    names = (first_name, middle_name, last_name)
    return ' '.join(name for name in names if name)

print(full_name(first_name='john', middle_name='lee', last_name='hooker'))
print(full_name(first_name='jimi', last_name='hendrix'))
print(full_name(first_name='madonna'))
print(full_name('john', 'lennon'))
Output:
john lee hooker jimi hendrix madonna Traceback (most recent call last): File "example.py", line 8, in <module> print(full_name('john', 'lennon')) TypeError: full_name() takes 0 positional arguments but 2 were given