Member Avatar for Member #1237233

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.

Dani AI

Generated

The function returns only the first letter because it executes a return inside the for loop — return exits the whole function on the first iteration. Move the return so the loop can process every character, and accumulate values as the loop runs (either by appending and summing later or by keeping a running total). Also watch for unreachable lines: code after an early return never runs.

A simple, clear approach is to build a fixed lookup (constant-time lookup per character) and add each letter's value to a running total. Non-letters can be ignored by letting the lookup return 0.

import string

_lookup = {c: i for i, c in enumerate(string.ascii_lowercase, 1)}

def get_numeric_value(name):
    total = 0
    for ch in name.lower():
        total += _lookup.get(ch, 0)
    return total

If a compact form is preferred, the same logic can be written as a generator expression:

def get_numeric_value(name):
    return sum(_lookup.get(ch, 0) for ch in name.lower())

Notes and edge cases: 's idea to normalize case before iterating is sound; it avoids separate upper/lower handling. Using str.find per character (as suggested there) works but each find scans the alphabet string, so a dict lookup is asymptotically cheaper (not important for short names). Decide how to treat hyphens, apostrophes, digits or accented letters: for Unicode accents consider unicodedata.normalize('NFKD', s) and strip combining marks before mapping, or explicitly define desired behavior. The core fix is removing the early return and summing every character's mapped value.

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.