Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to optimize a simple name matching script?

I apologize if this is a really simple question, but I have a rather long python script that I have no idea how to optimize. The script takes an input and places an alterative adjective before it.
Example: If input is "Adam" program would output "Awesome Adam".

Right now I have 26 nested elif functions. I know there is a simpler way to do this.
I assume there is a way to do this by using something like nameinput[0], but have python read it as the variable a rather than the string "A" and then having my list of adjectives be referenced by that letter.

Thank you so much in advance!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

a = "Awesome"
b = "Brave"
c = "Couragous"
d = "Daring"
e = "Energetic"
f = "Fun"
g = "Great"
h = "Heartfelt"
i = "Imaginative"
j = "Joyful"
k = "Kind"
l = "Lawful"
m = "Maternal"
n = "Nice"
o = "Open"
p = "Positive"
q = "Quirky"
r = "Resourceful"
s = "Smooth"
t = "Talkative"
u = "Understanding"
v = "Victorious"
w = "Wacky"
x = "Xenial"
y = "Young"
z = "Zany"
def name_casecheck(nameinput):
    if nameinput[0].isupper() == True:
        name(nameinput)
    else:
        print("Please capitalize your own name!")
        
        nameinput[0] = ()
def name(nameinput):
    if nameinput[0] == "A":
        print(a + " " + nameinput + "!")
    elif nameinput[0] == "B":
        print(b + " " + nameinput + "!")
    elif nameinput[0] == "C":
        print(c + " " + nameinput + "!")
    elif nameinput[0] == "D":
        print(d + " " + nameinput + "!")
    elif nameinput[0] == "E":
        print(e + " " + nameinput + "!")
    elif nameinput[0] == "F":
        print(f + " " + nameinput + "!")
    elif nameinput[0] == "G":
        print(g + " " + nameinput + "!")
    elif nameinput[0] == "H":
        print(h + " " + nameinput + "!")
    elif nameinput[0] == "I":
        print(i + " " + nameinput + "!")
    elif nameinput[0] == "J":
        print(j + " " + nameinput + "!")
    elif nameinput[0] == "L":
        print(k + " " + nameinput + "!")
    elif nameinput[0] == "M":
        print(m + " " + nameinput + "!")
    elif nameinput[0] == "N":
        print(n + " " + nameinput + "!")
    elif nameinput[0] == "O":
        print(o + " " + nameinput + "!")
    elif nameinput[0] == "P":
        print(p + " " + nameinput + "!")
    elif nameinput[0] == "Q":
        print(q + " " + nameinput + "!")
    elif nameinput[0] == "R":
        print(r + " " + nameinput + "!")
    elif nameinput[0] == "S":
        print(s + " " + nameinput + "!")
    elif nameinput[0] == "T":
        print(t + " " + nameinput + "!")
    elif nameinput[0] == "U":
        print(u + " " + nameinput + "!")
    elif nameinput[0] == "V":
        print(v + " " + nameinput + "!")
    elif nameinput[0] == "W":
        print(w + " " + nameinput + "!")
    elif nameinput[0] == "X":
        print(x + " " + nameinput + "!")
    elif nameinput[0] == "Y":
        print(y + " " + nameinput + "!")
    elif nameinput[0] == "Z":
        print(z + " " + nameinput + "!")
    else:
        print("I didn't quite understand that, try another name?")
        
        
name_casecheck("Andrew")

>Solution :

You can simplify your code a lot by using a dictionary!
Dictionaries are ways to relate keys to values – think of a list with names instead of indexes.
This way, you can use the first letter of the name to access the word you want:

word_to_add = {
    "a": "Awesome",
    "b": "Brave",
    "c": "Couragous",
    "d": "Daring",
    "e": "Energetic",
    "f": "Fun",
    "g": "Great",
    "h": "Heartfelt",
    "i": "Imaginative",
    "j": "Joyful",
    "k": "Kind",
    "l": "Lawful",
    "m": "Maternal",
    "n": "Nice",
    "o": "Open",
    "p": "Positive",
    "q": "Quirky",
    "r": "Resourceful",
    "s": "Smooth",
    "t": "Talkative",
    "u": "Understanding",
    "v": "Victorious",
    "w": "Wacky",
    "x": "Xenial",
    "y": "Young",
    "z": "Zany"
}

# I added a conversion to the first character so you won't have to capitalize your name (the .lower())
def name(nameinput):
    print(word_to_add[nameinput[0].lower()] + " " + nameinput + "!")


name("Andrew")

To shortly explain the changes:
word_to_add is a dictionary where the key is the first letter, and the value is the word to add (hence the name).
By getting the first letter of the word, and converting it to lowercase, we can make sure we don’t run into the capitalization issue.
Then, using the first letter as a key, we search for the corresponding word in the dictionary, and print it out.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading