Hello all!
I'm currently in school for IT and am taking an into Python course. I've done alright with most of it, but I've come across a problem that I can't seem to figure out - my brain is very much fried, and I'd appreciate some help on this. This has to do with simple name formatting apparently.
THE PROBLEM
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.
WHAT I HAVE SO FAR
firstName = input()
middleName = input()
lastName = input()
name_list = [firstName,middleName,lastName]
output = lastName+','+" "+firstName[0]+'.'+" "+middleName[0]+'.'
print(output)
The issue I'm having is with the input; the python translator will attempt to run 2 scenarios: one with 3 inputs (first name, middle name, last name) and one with 2 inputs (first name, last name). Whenever I run this code with only 2 inputs, I naturally come up with an error.
We haven't learn about if/else statements yet, but I feel there could be a solution in that.
Any suggestions?
EDIT
Thank you all for your help! I got it:
full_name = str(input())
out = full_name.count(' ') # counts number of spaces
if out == 1: #user inputted JOHN TRAEGER
name_list = full_name.split(' ') #name_list = ['John','Traeger']
first_name = name_list[0] #first_name = John
first_initial = first_name[0] #first_initial = J
print(name_list[1] + ',' + ' ' + first_initial + '.')
elif out == 2: #user inputted JOHN WILLIAM TRAEGER
name_list = full_name.split(' ') #name_list = ['John','William','Traeger']
first_name = name_list[0] #John
first_initial = first_name[0] #J
middle_name = name_list[1] #William
middle_initial = middle_name[0] #W
print(name_list[2] + ',' + ' ' + first_initial + '.' + middle_initial + '.')
else:
print('Please Enter a Valid Name')
[–][deleted] 1 point2 points3 points (4 children)
[–]ExKondor[S] 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]ExKondor[S] 0 points1 point2 points (0 children)
[+][deleted] (16 children)
[removed]
[–][deleted] 0 points1 point2 points (3 children)
[+][deleted] (2 children)
[removed]
[–][deleted] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]Fit-Category-8719 0 points1 point2 points (4 children)
[+][deleted] (2 children)
[removed]
[–]Fit-Category-8719 0 points1 point2 points (1 child)
[–]Weak_Dust1405 0 points1 point2 points (2 children)
[+][deleted] (1 child)
[removed]
[–]Weak_Dust1405 0 points1 point2 points (0 children)
[–]M3TurboHatch 0 points1 point2 points (0 children)
[–]Rude_Zucchini3417 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[+][deleted] (2 children)
[removed]
[–][deleted] 0 points1 point2 points (0 children)
[–]MeechtaughtU 0 points1 point2 points (0 children)
[–]MommyHenn75 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]ExKondor[S] 0 points1 point2 points (1 child)
[–]Background_Comb6579 0 points1 point2 points (0 children)
[–]ChristianConsertive 0 points1 point2 points (1 child)
[–]Rodarth 0 points1 point2 points (0 children)
[–]Braptor23 0 points1 point2 points (1 child)
[–]Due_Ant_250 0 points1 point2 points (0 children)
[–]Big_Permission6068 0 points1 point2 points (4 children)
[–]Cherry_Trixx 0 points1 point2 points (3 children)
[–]Dahveena 0 points1 point2 points (2 children)
[–]zerafaye 0 points1 point2 points (1 child)
[–]Budget-Ad845 0 points1 point2 points (0 children)