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. 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

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.

First of all, you don't need a to_numerical function. Instead of doing

for char in string:

do

for char in string.lower():

and if you have a const string 'abcdefghijklmnopqrstuvwxyz' you can find the one-relative value of the char by doing 'abcdefghijklmnopqrstuvwxyz'.find(char) + 1. That expression will evaluate to zero if the char is not a lower case letter.

The resulting get_numeric_value function is only five lines with no if statements required. Now all you need is to add a variable inside the loop to keep track of the total.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.