Welcome to the CSC Q&A.
Get help and give help!
Write great code!
It is our choices... that show what we truly are, far more than our abilities.


+6 votes

I was feeling romantic and I made a "name jumbler" to combine my last name with my partner's last name to see if we could find a joint name that we both liked. I managed to get the program to create a list of the letters in our names (with spaces taken out inside the program), but the problem is that when I remove the letters from the list, it appends the list that is used for the loop to an empty list, so we just get one name written into the file. I would like the program to write a designated number (in this case, 20) of names into the file for us to pick from.

Does anyone have any ideas how I can get the program to "go back" to the original list after the while loop ends?

The other thing I would like to have happen is to have the program not put two consonants next to each other ('dlnreosae' isn't an appealing last name and seeing it ruins the romance!!).

Here's the full code for you to see what I'm talking about because this isn't an assignment:

import random
vowels = ['a', 'e', 'i', 'o', 'u']

file = open("lastnames.txt", "w+")

def getNames():
    getNames = input('Enter the names you want to combine: ')
    getNames = getNames.lower()
    splitNames = list(getNames)

        while True:
            splitNames.remove(' ')
    except ValueError:
    return splitNames

savedNames = getNames()

for i in range(21):
    while len(savedNames) > 0:

        letter = random.choice(savedNames)

asked in CSC201 Spring 2021 by (4.6k points)

1 Answer

+2 votes

Personal side note: I am actually a "2nd generation name-merger". My parents' last names were "Samuelson" and "Hvistendahl", which they combined pieces of to create the new name "Sondahl". I was born Forrest Sondahl, but when I married a woman whose last name was "Stone", we merged names again to create "Stonedahl". I should probably mention that they would not let us get away with an unconventional name change like just by signing the marriage license -- instead we had to file paperwork and go to court for a "legal name change", a process which cost several hundred dollars.

Now, regarding your coding question, which appears to be looking for a complete anagram of all the letters of both names (which is different than the partial merging mentioned above).

Your current algorithm has some rather nasty-looking WHILE loops in it (please don't use while True: until an error occurs!), which are likely to cause bugs. If you can learn a few more list processing spells, I would recommend taking an alternative approach.

1) use the string .replace(...) method to replace all spaces with the empty string, and probably also make everything upper case.
2) convert the string to a list of letters using list(namesText)
3) use the random.shuffle(letterList) method to get all the letters in a random order
4) use the empty string to .join(...) the letterList together into one string.

In order to improve the pronouncibility of the resulting names, I would recommend making a separate function:

def scoreGoodness(candidateName):
    """returns a number score evaluating how 'good' a name might be for pronouncing.
       for example, add to the score for good letter combinations like vowel-consonent,
       or some special ones like "ch" or "th" or "st", and subtract a bunch from the score for
       likely pronunciation problems like three vowels or three consonents in a row"""

If you do this, then you can add step 5) up above, where you check whether a name has a sufficiently high goodness score BEFORE printing it out into your list of potential names.

answered by (10.1k points)

What a great story to add to my doe-eyed romance kick that I seem to be on (feel free to share more)!

I made this in the middle of the night, so honestly I'm surprised that it worked as well as it did. I'm definitely going to save this in my notes to come back to when I have more time. :)