7.1.8 Citation CodeHS Answers

We thoroughly check each answer to a question to provide you with the most correct answers. Found a mistake? Tell us about it through the REPORT button at the bottom of the page. Ctrl+F (Cmd+F) will help you a lot when searching through such a large set of questions.

The most common answer to the 7.1.8 Citation CodeHS is:

def citation(names):
    author_name = ((names))
    name = str(names[2]) + ", " + str(names[0]) + " "+  str(names[1])
    return name
print(citation(["Martin", "Luther", "King, Jr"]))

The function citation is intended to format an author’s name into a citation format. However, there’s an issue with the way it handles the names array, particularly with the assumption of the array structure and the handling of names like “King, Jr., Martin Luther”.

Let’s modify the function to handle an array of names more effectively:

def citation(names):
    # Assuming the names array is structured as [first_name, middle_name, last_name]
    if len(names) == 3:
        # For full names with a middle name
        formatted_name = f"{names[2]}, {names[0]} {names[1]}"
    elif len(names) == 2:
        # For names without a middle name
        formatted_name = f"{names[1]}, {names[0]}"
    else:
        # Fallback for any other case (like just a single name)
        formatted_name = names[0]

    return formatted_name

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

This function now:

  1. Checks the length of the names array to determine how to format the name.
  2. If there are three elements, it assumes the format [first_name, middle_name, last_name].
  3. If there are two elements, it assumes the format [first_name, last_name].
  4. For one element, it just returns the name as is.

This will handle a wider variety of name formats more robustly.

Was this helpful?

quizzma
Quizzma Team
+ posts

The Quizzma Team is a collective of experienced educators, subject matter experts, and content developers dedicated to providing accurate and high-quality educational resources. With a diverse range of expertise across various subjects, the team collaboratively reviews, creates, and publishes content to aid in learning and self-assessment.
Each piece of content undergoes a rigorous review process to ensure accuracy, relevance, and clarity. The Quizzma Team is committed to fostering a conducive learning environment for individuals and continually strives to provide reliable and valuable educational resources on a wide array of topics. Through collaborative effort and a shared passion for education, the Quizzma Team aims to contribute positively to the broader learning community.




Leave a Comment

Your email address will not be published. Required fields are marked *