list vs variables - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: list vs variables (/thread-7839.html) |
list vs variables - mcmxl22 - Jan-27-2018 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. What I want.
RE: list vs variables - j.crater - Jan-27-2018 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. RE: list vs variables - Gribouillis - Jan-27-2018 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! |