Python Forum

Full Version: list vs variables
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I need name to print out the assigned variables.
a = j = s = '1'
b = k = t = '2'
c = l = u = '3'
d = m = v = '4'
e = n = w = '5'
f = o = x = '6'
g = p = y = '7'
h = q = z = '8'
i = r = '9'

name = raw_input('What is your name? ')
for letter in name:
  print letter;
what I get.
Output:
What is your name? micah m i c a h
What I want.
Output:
What is your name? micah 4 9 3 1 8
You could make lists which include the letters, and number associated with those particular letters. Then in your loop you can check in which list the letter appears, and then fetch the number from that particular list.
You can try this function
>>> def number(letter):
...     return (ord(letter) - ord('a')) % 9 + 1
... 
>>> import string
>>> print([(l, number(l)) for l in string.ascii_lowercase])
[('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5), ('f', 6), ('g', 7), ('h', 8), ('i', 9), ('j', 1), ('k', 2), ('l', 3), ('m', 4), ('n', 5), ('o', 6), ('p', 7), ('q', 8), ('r', 9), ('s', 1), ('t', 2), ('u', 3), ('v', 4), ('w', 5), ('x', 6), ('y', 7), ('z', 8)]
Also check this!