Python Forum
TypeError: get_formatted_name() missing 1 required positional argument: 'last_name'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: get_formatted_name() missing 1 required positional argument: 'last_name'
#1
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!!
Reply
#2
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
Reply
#3
Thank you! That is exactly what I was looking for!
Reply
#4
(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
Reply
#5
@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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: a bytes-like object is required ZeroX 13 4,043 Jan-07-2023, 07:02 PM
Last Post: deanhystad
  Error TypeError: output_type_handler() takes 2 positional arguments but 6 were given paulo79 1 1,922 Oct-17-2022, 06:29 PM
Last Post: paulo79
  TypeError: a bytes-like object is required, not 'str' - Help Please. IanJ 3 4,773 Aug-29-2022, 05:53 PM
Last Post: deanhystad
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,837 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  Error: _vhstack_dispatcher() takes 1 positional argument but 9 were given alexfrol86 3 5,798 May-09-2022, 12:49 PM
Last Post: deanhystad
  What is positional argument self? Frankduc 22 5,671 Mar-06-2022, 01:18 AM
Last Post: Frankduc
  TypeError: missing a required argument: 'y' gible 0 2,894 Dec-15-2021, 02:21 AM
Last Post: gible
  positional argument: 'self' mcmxl22 8 3,267 Dec-13-2021, 10:11 PM
Last Post: deanhystad
  TypeError: missing 3 required positional arguments: wardancer84 9 10,822 Aug-19-2021, 04:27 PM
Last Post: deanhystad
  TypeError: run_oracle_job() missing 1 required positional argument: 'connection_strin python_student 1 1,961 Aug-06-2021, 08:05 PM
Last Post: SheeppOSU

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020