How to format a name

Question:

I’m taking a class on python and the lab assignment asks…

Many documents use a specific format for a person’s name. Write a program whose input is:

firstName middleName lastName

and whose output is:

lastName, firstInitial.middleInitial.

Ex: If the input is:

Pat Silly Doe

the output is:

Doe, P.S.

If the input has the form:

firstName lastName

the output is:

lastName, firstInitial.

Ex: If the input is:

Julia Clark

the output is:

Clark, J.

So far I have…

firstName = input()
middleName = input()
lastName = input()
lastName2 = input()
firstName2 = input()

print(lastName+ ',', (firstName[0])+'.'+(middleName[0])+'.')

on the test screen, it says it outputs as Doe, P.S. but when I go to the submit screen it says there’s no output. I don’t get any of this.

This is the error message I’m getting

0 / 3
Traceback (most recent call last):
  File "main.py", line 2, in <module>
    middleName = input()
EOFError: EOF when reading a line
Input
Pat Silly Doe
Your output
Your program produced no output
Expected output
Doe, P.S.
Asked By: jemcmorrow

||

Answers:

Maybe your problem is that you haven’t wrote the input.
I run it and it works just fine
[![That’s just running your code][1]][1]

However, I see a problem and it’s that you’re asking first nime twice and last name too and not really using them. Please let me know if I did help you
[1]: https://i.stack.imgur.com/b2jiq.png

Answered By: Y34x4t3

The question says that the input is all on one line. Each call to input() reads a whole line of input. So your solution requires 5 lines of input to be provided — 3 for a person with 3 names, and 2 for a second person with only 2 names.

You should just call input() once to get the whole name. Use split() to break it into a list at the whitespace separators. Then you can produce the appropriate output depending on the length of the list.

names = input().split()
if len(names) == 3:
    first_name, middle_name, last_name = names
    print(f'{last_name}, {first_name[0]}.{middle_name[0]}.')
elif len(names) == 2:
    first_name, last_name = names
    print(f'{last_name}, {first_name[0]}.')
else:
    print("Invalid input")
Answered By: Barmar

Instead of doing:

First_Name

Middle_Name

Last_Name

Put the input as:

First_Name Middle_Name Last_Name

I had my program written just fine when I did it, but I was putting the names on separate input lines, once I entered the entire name on ONE line for input, then it ran perfect.

Answered By: K_B
Below is my answer

full_name = input().split()

if len(full_name) == 2:
    first_name = full_name[0]
    last_name = full_name[1]
    print(f'{last_name}, {first_name[0]}.')
elif len(full_name) == 3:
    first_name = full_name[0]
    middle_name = full_name[1]
    last_name = full_name[2]
    print(f'{last_name}, {first_name[0]}.{middle_name[0]}.')
Answered By: Tuan Le
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.