简体   繁体   中英

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

I'm trying to make a encoder in python

Here is the code so far. It works fine but i want to change each letter in the string to the corresponding number eg: a=1, b=2. so then it would end up as a number sentence. My aim is to have 2 programs, one for encoding and one for decoding

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

Rather than using variables to store each letter, it would be more concise and easier to scale if you use a list and append each raw_input to the list.

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)

You could use a dict (dictionary) to store each letter and its corresponding number. This is flexible if you would like to change the code to something other than than the ordinal of the letter.

# 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]

You can use ord() method in python and do it.

word = raw_input()

print('Your code is\n')
for letter in word:
    print ord(letter)-96
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()

Output:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM