Python Forum

Full Version: Name Mashup Program Improvement in Python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am learning python and trying to solve different problems i came across this program which is name mashup which is in Get Programming:Learn to code with Python.

problem statement: Write a program that automatically combines two names given by the user. That’s an open-ended problem statement, so let’s add a few more details and restrictions: -Tell the user to give you two names in the format FIRST LAST. Show the user two possible new -names in the format FIRST LAST. -The new first name is a combination of the first names given by the user, and the new last -name is a combination of the last names given by the user. For example, if the user gives you -Alice Cat and Bob Dog, a possible mashup is Bolice Dot.

I'd like to know is there any way to improve this program like using functions to make this program better.

print('Welcome to the Name Mashup Game')

name1 = input('Enter one full name(FIRST LAST): ')
name2 = input('Enter second full name(FIRST LAST): ')

space = name1.find(" ")                    
name1_first = name1[0:space]
name1_last = name1[space+1:len(name1)]
space = name2.find(" ")
name2_first = name2[0:space]
name2_last = name2[space+1:len(name2)]
# find combinations
name1_first_1sthalf = name1_first[0:int(len(name1_first)/2)]
name1_first_2ndhalf = name1_first[int(len(name1_first)/2):len(name1_first)]
name1_last_1sthalf = name1_last[0:int(len(name1_last)/2)]
name1_last_2ndhalf = name1_last[int(len(name1_last)/2):len(name1_last)]

name2_first_1sthalf = name2_first[0:int(len(name2_first)/2)]
name2_first_2ndhalf = name2_first[int(len(name2_first)/2):len(name2_first)]
name2_last_1sthalf = name2_last[0:int(len(name2_last)/2)]
name2_last_2ndhalf = name2_last[int(len(name2_last)/2):len(name2_last)]

new_name1_first = name1_first_1sthalf + name2_first_2ndhalf
new_name1_last = name1_last_1sthalf + name2_last_2ndhalf
new_name2_first = name2_first_1sthalf + name1_first_2ndhalf
new_name2_last = name2_last_1sthalf + name1_last_2ndhalf

# print results
print(new_name1_first, new_name1_last)
print(new_name2_first, new_name2_last)
One can split input and construct new string on the fly (I didn't see any requirements how the mashup should be created), requires 3.6 <= Python for f-strings:

first_name = input('Enter full name (first, last): ' ).split()
second_name = input('Enter full name (first, last): ').split()

print(f"{first_name[0][0]}{second_name[0][1:]} {first_name[1][0]}{second_name[1][1:]}")
I already tried this split method but output is different than original output.I think requirement is that you have to half the string equally and swap the other halves with each string.splitting the full name into first and last name like Alice Cat and Bob Dog, a possible mashup is Bolice Dot.
“In the face of ambiguity, refuse the temptation to guess.”

I don’t see anywhere anything about halving (Alice Cat and Bob Dog, a possible mashup is Bolice Dot.)