-1

I want a user to enter a word e.g. apple and then convert each character in this string to a corresponding letter (a = 1, b = 2, c = 3 etc.)

So far I've defined all the letters

a=1
b=2
c=3
d=4 
e=5 
f=6 
g=7 
etc...

and have split the string to print out each letter using

word = str(raw_input("Enter a word: ").lower())

for i in range (len(word)):
    print word[i]

this prints the characters individually, but I can't figure out how to print these as their corresponding numbers, which I could then sum together.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Pad
  • 841
  • 2
  • 17
  • 45

9 Answers9

2

In your case, It is better to use a dictionary which defines all the characters and there value. The string library provides an easier way to do this. Using string.ascii_lowercase within a dict comprehension you can populate your dictionary mapping as such.

>>> import string
>>> wordmap = {x:y for x,y in zip(string.ascii_lowercase,range(1,27))}
>>> wordmap
{'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}

Now you can easily map this to your output. First we take the input

>>> word = str(raw_input("Enter a word: ").lower())
Enter a word: apple
>>> values = []

Now we just loop through the input word and append the values to an empty list. We append the values because we also need to find the sum of the values.

>>> for i in word:
...     print "{}".format(wordmap[i])
...     values.append(wordmap[i])
... 
1
16
16
12
5

You can finally use the sum function to output the sum total of the values.

>>> sum(values)
50
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
1

First of all you can loop over your string itself as its an iterator then you can get the expected id for your letters with ord(i)%96 :

word = str(raw_input("Enter a word: ").lower())

for i in word:
    print ord(i)%96

Note that ord('a')=97.

And for sum :

sum(ord(i)%96 for i in word)
Mazdak
  • 105,000
  • 18
  • 159
  • 188
1
>>> string = 'hello'
>>> for char in string:
...     print ord(char) - 96,
... 
8 5 12 12 15

You can get the sum as

>>> sum = 0
>>> for char in string:
...     sum += ord(char) - 96
... 
>>> sum
52

or lighter as

>>> sum ( [ ord(i) - 96 for i in string ] )
52
nu11p01n73R
  • 26,397
  • 3
  • 39
  • 52
1

If the values associated to letters is not necessary folowing the ASCII order values, a solution could be to store the letters and their corresponding value to a dict:

values = {
    'a':1,
    'b':2,
    'c':3,
    'd':4,
    'e':5,
    'f':6,
    'g':7,
    # etc.
}

word = str(raw_input("Enter a word: ").lower())

for i in range (len(word)):
    print values[word[i]]
Antwane
  • 20,760
  • 7
  • 51
  • 84
  • Hey! I saw your answer now. We both answered 23 secs apart and yet chose the same variable name! Perhaps this is what is called Knowledge congruency :) – Bhargav Rao Jun 04 '15 at 14:01
0

Use ord (converts a character to it's code):

def getnumber(letter)
    return ord(letter) - ord('a') + 1

and then, you can just do:

print sum(map(getnumber, word))
Emile
  • 2,946
  • 2
  • 19
  • 22
0

If you want the default ordinal of ANSI chars, you may use:

w = 'apple'
print [ ord(i) for i in w ]

If not, define your own mapping in a function, and call that in the list comprehension instead of the ord().

boardrider
  • 5,882
  • 7
  • 49
  • 86
0

Store the characters in a dict: dict['a'] = 1, etc.

ballade4op52
  • 2,142
  • 5
  • 27
  • 42
0

To get your current method working you could do this...

word = str(raw_input("Enter a word: ").lower())

for i in range (len(word)):
    # eval to change the string to an object
    print eval(str(word[i]))

This will replace the character string with the character object you have defined already.

But a better way to solver your problem might be something like this...

nums = map(lambda x: ord(x)-96, list(raw_input("Enter word: ").lower()))

This returns a list of all the letters numbered where a is 1, b is 2 ... y is 25, z is 26. You can then do for i in nums: print i to print all the numbers out line by line.

Songy
  • 851
  • 4
  • 17
0
word = raw_input("Enter a word: ").lower()
s=[list(i) for i in word.split()]
s=[x for _list in s for x in _list]
print s
for i in s:
    print ord(i)-96
Ajay
  • 5,267
  • 2
  • 23
  • 30