3
\$\begingroup\$

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)
\$\endgroup\$
3
\$\begingroup\$

Any time you're doing essentially the same thing (even if it's in different orders) to different values, it's a strong clue that the values should be a collection (like a list or tuple) rather than independent named variables. It'll usually make your code a lot shorter, and it makes it much much easier to extend -- what if you wanted to play the mashup game with more names?

from itertools import permutations
from typing import List

print('Welcome to the Name Mashup Game')

names = [
    input('Enter one full name(FIRST LAST): ').split(),
    input('Enter second full name(FIRST LAST): ').split(),
    # input('Enter third full name(FIRST LAST):' ).split(),  <- try me!
]

def mash(first: List[str], second: List[str]) -> List[str]:
    """
    Produce a list where each element has the first half of the corresponding
    element from the first list and the second half of the corresponding 
    element from the second list.

    For example:
        mash(['AA', 'BB'], ['XX', 'YY']) -> ['AX', 'BY']
    """
    return [
        a[:len(a)//2] + b[len(b)//2:]
        for a, b in zip(first, second)
    ]

for first, second in permutations(names, 2):
    print(' '.join(mash(first, second)))
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.