#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    11
    Rep Power
    0

    Adding up characters


    Hello all,

    I have another question about using strings to add up values.

    Find the numeric value of a name. The value is determined by summing up the values of the letters in their name. a=1, b=2, c=3 etc. with z=26. Construct a program that calculates the numeric sum of a complete name such as, John Jacob Jingleheimer Smith.

    I am confused as to how I would solve this example problem. This is what I have so far. I was thinking if maybe I could use the index of the letters but I don't know how to tell the program to add witch letters up...? Thanks for being patient with my lack of skill with programming.

    Code:
    import string
    
    def main():
    
    print "This program calculates the numeric value of a name."
    print
    letters = "0ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    name = raw_input("Enter a name: ")
    print "The value is", values
    main()
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    5
    Rep Power
    0
    Well I am new to python as well but I think you are going to want to setup a dictionary and set each letter to a given value. This is just a basic example of what I think you want to do. Unless whitespace has its own value you might want to string.strip() the input as well. Hope this helps.

    Code:
    dic = {'a':1, 'b':2, 'c':3}
    
    string = raw_input("Enter name: ")
    
    sumName = 0
    
    
    for char in string:
        sumName += dic[char]
    
    print sumName
    -AtomJay
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    11
    Rep Power
    0
    That worked like a charm, atomjay. Thanks very much! I am picking this stuff up at a very slow rate, so thank you for the help.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    A couple of refinements to AppleJay's post:

    1) force the string to lowercase before the loop, otherwise you will need to add the uppercase letters to the dictionary too.

    2) use dic.get(char, 0) instead of dic[char]. This will return 0 for characters that are not in the dictionary. If you do not do this the program will throw an exception at the first punctuation character it encounters.

    Dave - The Developers' Coach
    Last edited by DevCoach; February 7th, 2005 at 04:02 AM.
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    You can also use ord() to get the value of the character on the fly; you need to subtract 96 from the return value. For example:

    Code:
    >>> aString = 'John Jacob Jingleheimer Smith'
    >>> aString = aString.lower()
    >>>
    >>> value = 0
    >>>
    >>> for character in aString:
    ...     if not character.isspace(): value = value + ord(character) - 96
    ...
    >>> value
    262
    >>>
    This idea could also be used to construct the dictionary if you needed to do it the other way:

    Code:
    >>> dict([(chr(c), c - 96) for c in range(97, 123)])
    {'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4, 'g': 7, 'f': 6, 'i': 9, 'h': 8, 'k': 11, 'j': 10, 'm': 13, 'l': 12, 'o': 15, 'n': 14, 'q': 17, 'p': 16, 's': 19, 'r': 18, 'u': 21, 't': 20, 'w': 23, 'v': 22, 'y': 25, 'x': 24, 'z': 26}
    >>>
    Note: this is done using a list comprehension for compactness since I have Python 2.3 on this computer; in Python 2.4 I would do the same thing using generator expressions (remove the [] in this expression if you want to do this).

    Hope this helps,

    Mark.
    programming language development: www.netytan.com Hula

  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Just for completeness you could convert the first example above to a list comprehension, or generator expression like so:

    Code:
    >>> sum([ord(c) - 96 for c in aString if not c.isspace()])
    262
    >>>
    Mark.
    programming language development: www.netytan.com Hula

  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    11
    Rep Power
    0
    Thanks all for the added input.

IMN logo majestic logo threadwatch logo seochat tools logo