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.

I have a code, but it doesn't exactly work. Can you debug this:

# 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)

1 answer

The code you provided is almost correct and should work properly to collect names and print the full name. However, it could be improved slightly for clarity and user experience. Here’s the original code you provided, along with a cleaned-up version and minor enhancements:

Original Code:

# 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)

Debugged and Enhanced Version:

The original code is functional, but here are some modifications for better user prompts and input validation.

# Ask the user how many names they have
try:
    num_names = int(input("How many names do you have? "))
    
    # Ensure the number of names is positive
    if num_names < 1:
        print("You must have at least one name.")
    else:
        # 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.strip())  # Strip whitespace from the 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)

except ValueError:
    print("Please enter a valid number.")

Improvements Made:

  1. Input Validation: The program now checks if the number of names is at least 1, preventing invalid inputs.
  2. Whitespace Handling: name.strip() is used to remove any leading or trailing whitespace from the names entered by the user.
  3. Error Handling: A try-except block is added to catch non-integer inputs and provide a friendly message.

This ensures that the program operates smoothly and user inputs are handled correctly.