Search code examples
pythoncharacter-codes

How to add up the numeric values of character codes in python using a for loop


I have my own name saved as a string under the variable name. I want to find the character code for each character in my name and then add them all up using a for loop. This is what I've started with, no idea if it's staring about the right way

name = "Ashley Marie"
for index in name:
    ans = ord(index)

Solution

  • Kasra solution is the correct one, but you asked for a "for loop"...so here it is:

     name = "Ashley Marie"
        sum = 0
        for ch in map(ord, name):
            sum += ch
        print sum
    

    or

    name = "Ashley Marie"
    sum = 0
     for c in name:
        sum += ord(c)
    print sum