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

如何在python中使字符串的每个字母都成为变量,python,encoding,decoding,Python,Encoding,Decoding,我正在尝试用python制作一个编码器 这是到目前为止的代码。它工作正常,但我想将字符串中的每个字母都更改为相应的数字,例如:a=1,b=2。所以它最终会变成一个数字句子。我的目标是有两个程序,一个用于编码,一个用于解码 print "Welcome to the encoder" Letter1 = raw_input ("Please input the first letter of the word: ") Letter2 = raw_input ("Please input the s

我正在尝试用python制作一个编码器

这是到目前为止的代码。它工作正常,但我想将字符串中的每个字母都更改为相应的数字,例如:a=1,b=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
您可以在python中使用ord()方法并执行此操作

word = raw_input()

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

与其使用变量来存储每个字母,不如使用列表并将每个
raw\u输入添加到列表中,这样会更简洁、更易于扩展

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)

与其使用变量来存储每个字母,不如使用列表并将每个
raw\u输入添加到列表中,这样会更简洁、更易于扩展

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]

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

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
输出:

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

看起来这个问题在python2中,所以没有打印功能。使用
print ord(letter)-96,
将在一行上打印出来,就像问题中提供的代码试图在python2中打印一样,因此没有打印功能。使用
print ord(letter)-96,
会将其打印在一行上,就像问题中提供的代码试图执行的语法一样。我为缺少
而道歉。已修复。请注意,如果用户输入空值或多个字母,则将失败。我可以添加额外的答案验证,但这似乎更像是一个原型,让您开始一个想法,而不是生产代码。语法无效?我为丢失的
道歉。已修复。请注意,如果用户输入空值或多个字母,则将失败。我可以添加额外的答案验证,但这似乎更像是一个原型,让你开始一个想法,而不是生产代码。太好了,这是一个有趣的项目。我鼓励您继续使用它,编码和解码是优秀的编程实践@马特,我完全同意,很好,这是一个有趣的项目。我鼓励您继续使用它,编码和解码是优秀的编程实践@Matt我完全同意下面你可以找到我的解决方案@Bogdanarasputinlow你可以找到我的解决方案@Bogdanarasputinso如果你认为更好,你可以接受我的答案welcome如果你认为更好,你可以接受我的答案欢迎