For school I have to write a program that requests a 3 part name and breaks it into a first, middle, and last name. I wrote the code but the alignment of my output is not right. I was wonderng if someone can tell me what I did wrong and how to fix it. Any help would be appreciated. Thanks! I am a beginner at Python. The code has to be very simple like for a kidnergartener and it has to follow the format of the code I already wrote and has to work with any name I type in and has to use the .find() function. No loops or if else statements can be used.

def main():
name=input("Please enter a full name with 3 names, separated by one space: ")
space=name.find(" ")
first=name[:space]
middle=name[space+1:]
secondname=middle[:space]
last=middle[space:]
print(first)
print(secondname)
print(last)

main()

Output:
Please enter a full name with 3 names, separated by one space: francis ford coppola
francis
ford co
ppola

Please enter a full name with 3 names, separated by one space: billy dee williams
billy
dee w
illiams

Recommended Answers

All 2 Replies

Correct indention would help a lot. You would learn by figuring it out yourself, here anyway minimal change version that works (raw_input if you use Python2 otherwise keep input):

def names():
	name=raw_input("Please enter a full name with 3 names, separated by one space: ")
	space=name.find(" ")
	first=name[:space]
	last=name.rfind(" ")
	middle=name[space+1:last]
	lastname=name[last+1:]
	print(first)
	print(middle)
	print(lastname)
names()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.