如何使字符串的每个字母成为 python 中的变量

How to make each letter of a string a variable in python

我正在尝试在 python

中制作一个编码器

这是目前为止的代码。它工作正常,但我想将字符串中的每个字母更改为相应的数字,例如:a=1,b=2。那么它最终会成为一个数字句子。我的目标是有2个程序,一个用于编码,一个用于解码

print "Welcome to the encoder"
Letter1 = raw_input ("Please input the first letter of the word: ")
Letter2 = raw_input ("Please input the second letter of the word: ")
Letter3 = raw_input ("Please input the third letter of the word: ")
Letter4 = raw_input ("Please input the fourth letter of the word: ")
Letter5 = raw_input ("Please input the fifth letter of the word: ")
Letter6 = raw_input ("Please input the sixth letter of the word: ")
Letter7 = raw_input ("Please input the seventh letter of the word: ")
Letter8 = raw_input ("Please input the eighth letter of the word: ")
Letter9 = raw_input ("Please input the ninth letter of the word: ")
Letter10 = raw_input ("Please input the tenth letter of the word: ")
Letter11 = raw_input ("Please input the eleventh letter of the word: ")
Letter12 = raw_input ("Please input the twelvth letter of the word: ")
print "Your code is " + Letter3 + Letter2 + Letter1 + Letter6 + Letter5 
+ Letter4 + Letter9 + Letter8 + Letter7 + Letter12 + Letter11 + 
Letter10

你可以在python中使用ord()方法并执行它。

word = raw_input()

print('Your code is\n')
for letter in word:
    print ord(letter)-96

与其使用变量来存储每个字母,不如使用列表并将每个 raw_input 追加到列表中,这样会更简洁且更易于扩展。

print "Welcome to the encoder"
code_length = 12
code = []
for index in xrange(0, code_length):
    letter = raw_input  ("Please input letter " + str(index) + " of the word: ")
    ordinal = ord(letter) - 96
    code.append(str(ordinal))

print ' '.join(code)

您可以使用 dict(字典)来存储每个字母及其对应的数字。如果您想将代码更改为字母序号以外的代码,这很灵活。

# define the dictionary
encoder = {"a":"1", "b":"2", "c":"3", "d":"4", "e":"5"}

# take your input
Letter1 = raw_input ("Please input the first letter of the word: ")
Letter2 = raw_input ("Please input the first letter of the word: ")
Letter3 = raw_input ("Please input the first letter of the word: ")

# print out the encoded version
print encoder[Letter1] + encoder[Letter2] + encoder[Letter3]
Letter1,Letter2,Letter3,Letter4,Letter5,Letter6,Letter7,Letter8,Letter9,Letter10,Letter11,Letter12=[''],[''],[''],[''],[''],[''],[''],[''],[''],[''],[''],['']
def encoder():
  print "Welcome to the encoder"
  [Letter1.append(str(ord(c)-96)) for c in raw_input ("Please input the first letter of the word: ")]
  [Letter2.append(str(ord(c)-96)) for c in raw_input ("Please input the second letter of the word: ")]
  [Letter3.append(str(ord(c)-96)) for c in raw_input ("Please input the third letter of the word: ")]
  [Letter4.append(str(ord(c)-96)) for c in raw_input ("Please input the fourth letter of the word: ")]
  [Letter5.append(str(ord(c)-96)) for c in raw_input ("Please input the fifth letter of the word: ")]
  [Letter6.append(str(ord(c)-96)) for c in raw_input ("Please input the sixth letter of the word: ")]
  [Letter7.append(str(ord(c)-96)) for c in raw_input ("Please input the seventh letter of the word: ")]
  [Letter8.append(str(ord(c)-96)) for c in raw_input ("Please input the eighth letter of the word: ")]
  [Letter9.append(str(ord(c)-96)) for c in raw_input ("Please input the ninth letter of the word: ")]
  [Letter10.append(str(ord(c)-96)) for c in raw_input ("Please input the tenth letter of the word: ")]
  [Letter11.append(str(ord(c)-96)) for c in raw_input ("Please input the eleventh letter of the word: ")]
  [Letter12.append(str(ord(c)-96)) for c in raw_input ("Please input the twelvth letter of the word: ")]
  print "Your code is " + ''.join(Letter3) + ''.join(Letter2) + ''.join(Letter1) + ''.join(Letter6) + ''.join(Letter5) + ''.join(Letter4) + ''.join(Letter9) + ''.join(Letter8) + ''.join(Letter7) + ''.join(Letter12) + ''.join(Letter11) + ''.join(Letter10)

def decoder():
  Letter112 = [chr(int(c)+96) for c in Letter1[1:]]
  Letter21 = [chr(int(c)+96) for c in Letter2[1:]]
  Letter31 = [chr(int(c)+96) for c in Letter3[1:]]
  Letter41 = [chr(int(c)+96) for c in Letter4[1:]]
  Letter51 = [chr(int(c)+96) for c in Letter5[1:]]
  Letter61 = [chr(int(c)+96) for c in Letter6[1:]]
  Letter71 = [chr(int(c)+96) for c in Letter7[1:]]
  Letter81 = [chr(int(c)+96) for c in Letter8[1:]]
  Letter91 = [chr(int(c)+96) for c in Letter9[1:]]
  Letter101 = [chr(int(c)+96) for c in Letter10[1:]]
  Letter111 = [chr(int(c)+96) for c in Letter11[1:]]
  Letter121 = [chr(int(c)+96) for c in Letter12[1:]]
  print "Your code is " + ''.join(Letter31) + ''.join(Letter21) + ''.join(Letter112) + ''.join(Letter61) + ''.join(Letter51) + ''.join(Letter41) + ''.join(Letter91) + ''.join(Letter81) + ''.join(Letter71) + ''.join(Letter121) + ''.join(Letter111) + ''.join(Letter101)

encoder()
decoder()

输出:

Welcome to the encoder
Please input the first letter of the word:  asdf
Please input the second letter of the word:  b
Please input the third letter of the word:  g
Please input the fourth letter of the word:  r
Please input the fifth letter of the word:  d
Please input the sixth letter of the word:  w
Please input the seventh letter of the word:  r
Please input the eighth letter of the word:  c
Please input the ninth letter of the word:  g
Please input the tenth letter of the word:  d
Please input the eleventh letter of the word:  s
Please input the twelvth letter of the word:  c
Your code is 72119462341873183194
Your code is gbasdfwdrgcrcsd