Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

I just don't understand how to get this to print the way I want it to

+5 votes
asked Mar 16, 2022 by Joey (540 points)
firstName = input()
middleName = input()
lastName = input()
#middleInitial
#firstInitial

middleInitial=middleName[0]
firstInitial=firstName[0]

print(lastName,',',end='')
print(firstInitial,'.',middleInitial,'.')
This is just an example, the problem I have is getting the output to print the way I want it to.
Pat  <<input
Silly  <<input
Doe  <<input
Doe ,P . S .  <<output
^^^this is what my program produces.

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. << expected output.
I've included the assignment that is attached to this particular bit of code for clarification. Any help would be appreciated

2 Answers

0 votes
answered Mar 16, 2022 by Peter Minarik (84,720 points)

The problem is that in the print() function you list everything you want to print separated by a comma (,). So Python prints all of those things automatically separated by white space.

Instead, you should print only one string to control how things are displayed. Take your initial string and concatenate any following string to it with the plus (+) operator:

print(lastName + ', ' + firstInitial + '.' + middleInitial + '.')

I'd also double-check where you are required to put a space and where not. That is, normally I would not expect "Doe, P.S." to be the output but "Doe, P. S." (notice the space before the S). Of course, the person who made the assignment could have different ideas. :)

commented Mar 16, 2022 by Joey (540 points)
Makes sense. I'll give it a shot, and it's greatly appreciated
commented Mar 16, 2022 by Joey (540 points)
now I'm getting an EOFError:
Traceback (most recent call last):
  File "main.py", line 3, in <module>
    middleName = input()
EOFError: EOF when reading a line
commented Mar 17, 2022 by Peter Minarik (84,720 points)
Can you share the whole code as it's not clear what's going on?
commented Mar 18, 2022 by Joey (540 points)
of course, mate. give us a tick. aaaaand :
''' Type your code here. '''
firstName = input()
middleName = input()
lastName = input()
#middleInitial
#firstInitial

middleInitial=middleName[0]
firstInitial=firstName[0]

print(lastName + ', ' + firstInitial + '.' + middleInitial + '.')

^^^ The code given to me at the start.

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 Sally 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.

^^^^ These are the instructions given.
commented Mar 18, 2022 by Joey (540 points)
I don't really want the answer given to me but maybe an explanation or a nudge in the right direction would really help me out. I've been at this for a month and my deadline is coming up. I'm just not understanding the material fast enough. I might get out this semester and try and teach this to myself instead of giving it the ol college try. I learn better by doing not by sitting in a classroom ya know?
commented Mar 18, 2022 by Joey (540 points)
Scratch that. There was ZERO code given. I just hit the load default template on my page and it erased everything except for the ''' ''' thing at the top
commented Mar 18, 2022 by Peter Minarik (84,720 points)
With the code shared (basically what I suggested to you before) I do not experience the error you said (EOFError).

Can you save your project and share a link to your saved project instead?
commented Mar 18, 2022 by Joey (540 points)
I'm not sure, as it is on Zybooks, and I am VERY green with zybooks
0 votes
answered Mar 16, 2022 by Jacob Buck (240 points)
reshown Mar 16, 2022 by Jacob Buck
Hello, how about you give this code a try, it gives you exactly what you're wanting and is a little simpler also, if you have any questions feel free to ask.

firstName = input("Firstname: ")
middleName = input("Middlename: ")
lastName = input("Lastname: ")

middleInitial = middleName[0]
firstInitial = firstName[0]

print(f"{lastName}, {firstInitial}.{middleInitial}.")
commented Mar 17, 2022 by Peter Minarik (84,720 points)
Yeah, string interpolation (or f-string) is also a good idea.
commented Mar 18, 2022 by Joey (540 points)
Even with your code it pops the EOFError
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...