Solved:

Checkmark

Answered by AI, Verified by Human Experts

Hi, I'm working on "How many Names" in CodeHS(python) right now and am stuck on some code. Is there anything I need to add or change or remove?

Hi, I'm working on "How many Names" in CodeHS(python) right now and am stuck on some code. Is there anything I need to add or change or remove?Here are the directions:

Some people have just a first name and a last name. Some people also have a middle name. Some people have five middle names.

Write a program that asks the user how many names they have. (If they have a first name, two middle names, and a last name, for example, they would type 4.) Then, using a for loop, ask the user for each of their names. Store the names in a list.

Then, use slices to separately print the person’s first name, middle name(s), and last name.

My Code is this:

fullname = " "
names = int(input("how many names do you have: "))
for i in range (names):
cheese = input("what is your name: ")
fullname = fullname +" "+ cheese
print ("First name:" + str(cheese))
print ("Middle names:" + str(cheese))
print ("Last name:" + str(cheese))

Your code correctly prompts the user for the number of names they have and then uses a for loop to collect their names and store them in the variable "fullname". However, you will have to do some adjustments to print each of the user's names as their first name, middle name(s), and last name.One way to do this is to store each name as an element in a list and then use list slicing to access the first, middle, and last elements. Here's an example of how you can modify your code to achieve this:names = int(input("how many names do you have: "))name_list = []for i in range (names):cheese = input("what is your name: ")name_list.append(cheese)print("First name: " + name_list[0])print("Middle names: " + " ".join(name_list[1:-1]))print("Last name: " + name_list[-1])In this example, the input names are stored in a list called name_list. By using list slicing and the join() method you can print each part of the name separately....

Unlock full access for 72 hours, watch your grades skyrocket.
For just $0.99 cents, get access to the powerful quizwhiz chrome extension that automatically solves your homework using AI. Subscription renews at $5.99/week.