0

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

3
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by pyTony
0

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

Edited by pyTony: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.