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

时间:2018-03-08 05:39:26

标签: python encoding decoding

我试图在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

4 个答案:

答案 0 :(得分:2)

如果您使用列表并将每个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)

答案 1 :(得分:1)

您可以使用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]

答案 2 :(得分:0)

您可以在python中使用ord()方法并执行此操作。

word = raw_input()

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

答案 3 :(得分:0)

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