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 in order (Ex: first name, middle name 1, middle name 2, last name). Finally, print their full name using the user's input from the saved string.

1 answer

Certainly! Below is an example of a Python program that accomplishes what you've described. The program prompts the user for the number of names they have, collects each name using a loop, and finally prints out the full name.

# Ask the user how many names they have
num_names = int(input("How many names do you have? "))

# Initialize an empty list to hold the names
names = []

# Use a for loop to collect each name
for i in range(num_names):
    name = input(f"Please enter name {i + 1}: ")
    names.append(name)

# Join the names into a single string to form the full name
full_name = ' '.join(names)

# Print the full name
print("Your full name is:", full_name)

How to Use the Program:

  1. Run the program.
  2. Enter the total number of names you want to input.
  3. Enter each name in the order you've specified.
  4. The program will output your full name concatenated from the names provided.

Example Interaction:

How many names do you have? 4
Please enter name 1: John
Please enter name 2: Michael
Please enter name 3: David
Please enter name 4: Doe
Your full name is: John Michael David Doe

This simple program captures any number of name components and formats them correctly.