Hi everyone,
I really need help to make the below code calculate the numeric value for each name separately.
for example: if i enter "Williams smith", it will calculate the first name and give its value alone and calculate the second name and give its value too. the code right now gives only the total value for the complete name.

thanks in advance

def main():
 
    print "This program calculates a numeric value for a name.\n"
    name = raw_input("Please enter your first or last name (no spaces): ").lower()
    print "\n","-"*10
    total = 0
    for ch in name:
        total += ord(ch)-96
        print ch,"=",ord(ch)-96
 
    print "-"*10,"\n",name,"=",total
 
main ()

The simplest way I would do it is check if "ch" was a blank space and then just skip it.

Your program, however, states to not use a space and to provide a first or last name only.

Example 1(ignore space):

def main():
 
    print "This program calculates a numeric value for a name.\n"
    name = raw_input("Please enter your first or last name (no spaces): ").lower()
    print "\n","-"*10
    total = 0
    for ch in name:
        #check for blank char
        if ch == " ":
            continue #continue the for loop from the beginning
        total += ord(ch)-96
        print ch,"=",ord(ch)-96
 
    print "-"*10,"\n",name,"=",total
 
main ()

Example 2 (confine to your specifications):

import sys

def main():

    print "This program calculates a numeric value for a name.\n"
    #put everthing in a while loop so we can check the input
    while True:
        name = raw_input("Please enter your first or last name (no spaces): ").lower()
        #check if there is a space
        if " " in name:
            #if so, inform user and continue from begining of while loop
            print "\nThere was a space detected in the supplied value.\nTry again.\n"
            continue

        else:
            print "\n","-"*10
            total = 0
            for ch in name:
                total += ord(ch)-96
                print ch,"=",ord(ch)-96
            print str(total)+"\n"
            #added sys.exit to exit program after we get the right values
            sys.exit()

main()

The simplest way I would do it is check if "ch" was a blank space and then just skip it.

Your program, however, states to not use a space and to provide a first or last name only.

Example 1(ignore space):

def main():
 
    print "This program calculates a numeric value for a name.\n"
    name = raw_input("Please enter your first or last name (no spaces): ").lower()
    print "\n","-"*10
    total = 0
    for ch in name:
        #check for blank char
        if ch == " ":
            continue #continue the for loop from the beginning
        total += ord(ch)-96
        print ch,"=",ord(ch)-96
 
    print "-"*10,"\n",name,"=",total
 
main ()

thanks for answer, but this code gives exactly the same output as my code did.

This program calculates a numeric value for a name.

Please enter your first or last name (no spaces): Williams smith

----------
w = 23
i = 9
l = 12
l = 12
i = 9
a = 1
m = 13
s = 19
s = 19
m = 13
i = 9
t = 20
h = 8
---------- 
williams smith = 167

what i need is to give each name's value separately first then the total.
for example, the output should be like this:

----------
williams = 98
smith = 69

williams smith = 167
----------

My Beginners' Solution:

def main():
    print "This program calculates a numeric value for a name.\n"
    first, last = raw_input("Enter your first and last name (with a space): ").lower().split()
    print "\n","-"*10
    totalFirst = 0
    totalLast = 0
    for ch in first:
        totalFirst += ord(ch)-96
        print ch,"=",ord(ch)-96

    for ch in last:
        totalLast += ord(ch)-96
        print ch,"=",ord(ch)-96

    print "-"*10,"\n",first,"=",totalFirst
    print "-"*10,"\n",last,"=",totalLast
    print "-"*10,"\n",first,last, "=",totalFirst+totalLast

main ()

Edit Timed Out!!!

Heres the result:

This program calculates a numeric value for a name.


----------
w = 23
i = 9
l = 12
l = 12
i = 9
a = 1
m = 13
s = 19
s = 19
m = 13
i = 9
t = 20
h = 8
----------
williams = 98
----------
smith = 69
----------
williams smith = 167

Some note.
For better look/style than just stuff all code in a main function,it can be better to make som more functions.
Then bring it all togeher in the main function.
Here you also see that i use docstring in function name_calc.

def name_input():
    name = raw_input("Please enter your first or last name (no spaces): ").lower()
    first,last = name.split()
    return first, last

def name_calc(first,last):
    '''Calculate numeric value from input names'''
    first_calc = sum([ord(c)-96 for c in first])
    last_calc = sum([ord(c)-96 for c in last])
    return first_calc, last_calc

def main():
    first,last = name_input()
    f, l = name_calc(first,last)
    print '%s(%d) %s(%d) total numeric value %s' %  (first,f, last,l, f+l)
    #williams(98) smith(69) total numeric value 167

if __name__ == '__main__':
    main()

I would refactor little snipsat's code and remove the unnecessary list comprehension and replace it with generator. I prefer to do calculation to all name again to using temporary variables, as operation is instantaneous.

def name_input():
    name = ''
    while ' ' not in name:
        name = raw_input("Please enter your first and last name: ").lower()        
    return name.split()

def name_calc(name):
    '''Calculate numeric value from input name'''
    return sum(ord(c) - 96 for c in name if c.isalpha())

def main():
    first, last = name_input()
    print '%s(%d) %s(%d) total numeric value %s' %  (first, name_calc(first),
                                                     last, name_calc(last),
                                                     name_calc(first + last))
    #williams(98) smith(69) total numeric value 167

main()

The simplest way I would do it is check if "ch" was a blank space and then just skip it.

Your program, however, states to not use a space and to provide a first or last name only.

Example 1(ignore space):

def main():
 
    print "This program calculates a numeric value for a name.\n"
    name = raw_input("Please enter your first or last name (no spaces): ").lower()
    print "\n","-"*10
    total = 0
    for ch in name:
        #check for blank char
        if ch == " ":
            continue #continue the for loop from the beginning
        total += ord(ch)-96
        print ch,"=",ord(ch)-96
 
    print "-"*10,"\n",name,"=",total
 
main ()

thanks for answer, but this code gives exactly the same output as my code did.

This program calculates a numeric value for a name.

Please enter your first or last name (no spaces): Williams smith

----------
w = 23
i = 9
l = 12
l = 12
i = 9
a = 1
m = 13
s = 19
s = 19
m = 13
i = 9
t = 20
h = 8
---------- 
williams smith = 167

what i need is to give each name's value separately first then the total.
for example, the output should be like this:

----------
williams = 98
smith = 69

williams smith = 167
----------

Your original code used the space " " in the total. Making williams smith=103. What I did was check and make sure the space was not present in the total value.

You should probably use pyTony or snippsat approach over mine though. They are much cleaner and could be incorporated into a class better.

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.