Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list vs variables
#1
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
Reply
#2
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.
Reply
#3
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Advancing Through Variables In A List knight2000 0 514 May-13-2023, 03:30 AM
Last Post: knight2000
  Converting list to variables Palves 1 1,761 Sep-18-2020, 05:43 PM
Last Post: stullis
  Print variable values from a list of variables xnightwingx 3 2,615 Sep-01-2020, 02:56 PM
Last Post: deanhystad
  Using a list of variables Zane217 6 2,516 Jun-09-2020, 01:37 PM
Last Post: Yoriz
  add all variables to a list faszination_92 6 3,092 Apr-14-2020, 04:36 AM
Last Post: buran
  Creating a List with many variables in a simple way donnertrud 1 2,029 Jan-11-2020, 03:00 PM
Last Post: Clunk_Head
  2D Array/List OR using variables in other variable names? IAMK 4 3,839 Apr-16-2018, 09:09 PM
Last Post: IAMK
  list of user's variables in the interpreter nzcan 5 3,884 Jan-21-2018, 11:02 AM
Last Post: nzcan

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020