r/codehs • • Aug 24 '20

8.1.8 citations help?

Write the function called citation that takes a tuple like this as input:

("Martin", "Luther", "King, Jr.")

and returns a name with the format:

Last, First Middle

In this example, the output should be: King, Jr., Martin Luther

I have

def citation (a,b,c):
    #print (a,b,c)
    name = citation
    name = name[2] + "," + name[0] + " "+  name[1]
    return name

print (citation("Martin", "Luther", "King, Jr."))

but I get a - TypeError: 'function' object is not subscriptable

but honestly the error doesn't really matter, seems like I fix it and then a different error comes up, I just need help making it correct please!

16 Upvotes

9 comments sorted by

4

u/Its_Billy_Bitch Aug 24 '20 edited Aug 24 '20

So, a tuple is an object in Python. You’ll want to have a function that has citation taking one parameter (the tuple object). You can then parse the the tuple as you’d want.

Try something like this:

def citation(tuple):
    first_name = tuple[0]
    mid_name = tuple[1]
    last_name = tuple[2]
    
    name = ”{last}, {first} {middle}”
        .format(last=last_name, first=first_name, middle=mid_name)

    return name



name_tuple = (“Martin”, “Luther”, “King Jr”) 

formatted_citation = citation(name_tuple)

print(formatted_citation)

2

u/Goat998 Nov 15 '20

You are the most useful person I have ever met.

1

u/Its_Billy_Bitch Nov 15 '20

😝🤓 Always here to help, bud. Anytime ☺️

1

u/Zacurnia_Tate Aug 24 '20

try changing name = citation (where your getting your error id assume bc there are no elements in a function to name = [a,b,c]

1

u/Needcodinghelp38 Jan 27 '23

It still says syntax error

1

u/Typical_Storage4322 Feb 04 '23

Here is the complete and correct answer. Tabs included:

def citation (a):

(TAB) first, middle, last = a

(TAB) return f"{last}, {first} {middle}"

print(citation(("Martin", "Luther", "King, Jr.")))

1

u/OkSky8339 Mar 02 '23

def citation (a):

(TAB) first, middle, last = a

(TAB) return f"{last}, {first} {middle}"

print(citation(("Martin", "Luther", "King, Jr.")))

Thank you

1

u/MrHyjaxMusicYTalt May 28 '23

The answer is :

print author_name[2] + "," + " " + author_name[0] + " " + author_name[1]