#Caesar cipher
#alphabets is dictionary
alphabets={'a':'1', 'b':'2' , 'c':'3' , 'd':'4' , 'e':'5' , 'f':'6' , 'g':'7' , 'h':'8' , 'i':'9' , 'j':'10' , 'k':'11' , 'l':'12' , 'm':'13' , 'n':'14' , 'o':'15' , 'p':'16' , 'q':'17' , 'r':'18' , 's':'19' , 't':'20' , 'u':'21' , 'v':'22' , 'w':'23' , 'x':'24' , 'y':'25' , 'z':'26' }
#input of key
key=(input("Enter the key for cypher\t"))
#encoding program starts
#msg input
msg=input('Enter the message you want to encode\n')
#I used to assume the input as 'hi'
#make it into a list
letters=[]
for i in msg:
letters.append(i)
list(letters)
numbers=[]
for i in range(0,2):
numbers.append(alphabets[letters[i+key]])
print(numbers)
如果您想转移字符购买给定的数字,则可以使用ord
和chr
,不需要使用字典。
ord('a') # gives you 97 (ASCII)
因此,例如,将一个字符移动2,您可以执行以下操作
key = 2
char(ord('a') + key)) # gives you 'c'
现在您可以遍历味精并转换每个字符。