Get the Sum of the Values of Letters in a Name

Question:

Numerologists claim to be able to determine a person’s character traits based on the "numeric value" of a name. The value of a name is determined by summing up the values of the letters of the name. For example, the name Zelle would have the value 26 + 5 + 12 + 12 + 5 = 60. Write a function called get_numeric_value that takes a sequence of characters (a string) as a parameter. It should return the numeric value of that string. You can use to_numerical in your solution.

First, my code for to_numerical (a function I had to make to convert letters to numbers [notably not the ASCII codes; had to start from A = 1, etc.]) was this:

def to_numerical(character): 
    if character.isupper():
        return ord(character) - 64
    elif character.islower():
        return ord(character) - 96

In regards to the actual problem, I’m stuck since I can only get the function to return the value of the Z in Zelle (which is supposed to be 26 here). I’ve pasted what I have so far below:

def get_numeric_value(string):
    numerals = []
    for character in string:
        if character.isupper():
            return ord(character) - 64
        elif character.islower():
            return ord(character) - 96
        return numerals
    addition = sum(numerals)
    return addition

I was thinking I would probably have to use sum() at some point, but I think the issue is more that I’m not getting all of the letters returned to the numerals list. How can I get it so the code will add up and return all the letters? I’ve been trying to think something up for an hour but I’m stumped.

Asked By: Felix

||

Answers:

You should just use a sum variable and add to it each character value and then return that sum:

def get_numeric_value(string):
    numerals = []
    sum = 0
    for character in string:
        if character.isupper():
            sum += ord(character) - 64
        elif character.islower():
            sum += ord(character) - 96
    return sum
Answered By: Bemwa Malak

The problem is that you are using return multiple times, each time the program reaches a return it stops the function and returns the specified value.

In your case the first letter fulfills one of the conditionals (specifically character.isupper(), returns the value of the letter and the program ends.

I think you wanted to use the .append() method, this method allows you to add elements to a list, leaving something like this:

def get_numeric_value(string):
    numerals = []
    for character in string:
        if character.isupper():
            numerals.append(ord(character) - 64)
        elif character.islower():
            numerals.append(ord(character) - 96)

    addition = sum(numerals)
    return addition

You can also use the to_numeric function you declared earlier, giving a much cleaner result (in my opinion).

def get_numeric_value(string):
    numerals = []
    for character in string:
        numerals.append(to_numerical(character))

    addition = sum(numerals)
    return addition

If you want it to be just one line you can use something called list comprehension, though you sacrifice some readability.

def get_numeric_value(string):
    return sum([to_numerical(character) for character in string])

This one line answer should work as well:

def get_numeric_value(string):
    return sum(ord(character) - (64 if character.isupper() else 96) for character in string if character.isalpha())
Answered By: ErnestBidouille

why not just first lowecase the string to get rid of all the if-else blocks?
since all the characters are now lowercase we can use this function:

def get_numeric_value(string:str) -> int:
    return sum([ord(c) - 96 for c in string.lower() if c.isalpha()])
Answered By: amirali mollaei
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.