Python Forum
Name Mashup Program Improvement in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Name Mashup Program Improvement in Python
#1
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)
Reply
#2
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'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
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.
Reply
#4
“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.)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Random coordinate generator speed improvement saidc 0 2,059 Aug-01-2021, 11:09 PM
Last Post: saidc
  Function Improvement Santino 1 1,813 May-23-2020, 03:30 PM
Last Post: jefsummers
  first try with python class, suggestion for improvement please anna 18 5,943 Nov-01-2019, 11:16 AM
Last Post: anna
  coding improvement tips vaisesumit29 1 1,818 Mar-10-2019, 05:09 PM
Last Post: stullis
  Web Scraping efficiency improvement HiImNew 0 2,395 Jun-01-2018, 08:52 PM
Last Post: HiImNew
  Router interface status through SNMP - suggestion required for improvement. anna 0 3,039 Feb-27-2018, 11:10 AM
Last Post: anna
  working code, suggestion required for improvement anna 18 8,516 Dec-29-2017, 01:24 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020