在凯撒cypher在Python [英] Caesar cypher in Python

查看:387
本文介绍了在凯撒cypher在Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手,我想编写一个程序,要求输入一行(每个数字将被一个空格分开)。它将使用一个简单的字母数字替换密码。每封信将被指定一个数字。所以1 = a,2 = b和3 = c,直到它达到26 = z。然而,从那里,密码将继续下去; 27 = a,28 = b等0将是一个空格。程序只能使用0和正数。它还会打印消息的解密。例如:



请键入代码: 8 5 12 12 15





另一个例子:



请输入代码: 16 25 20 8 14 0 9 19 0 3 15 15 12



python很酷



字母表成这样的列表;

  n = int(input(请输入代码:)
x = [ 'A', 'b', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', '0', 'p', 'q', 'R', 'S', 'T', 'U', 'v', 'W', 'X','Y ','z']

...然后再回来看,我也试过使用.replace()函数,像这样:

  n = int (输入(请键入代码:)
n = n.replace('1','a')#all直到z然后循环它的方式
print(n)

等等,但是再一次,我不知道如何正确地执行此操作
任何帮助将不胜感激。谢谢。

解决方案

使用 split()

  numbers = input(请输入代码:).split()

#['16','25 ','20','8','14','0','9',',19','0','3','15','15' b

使用 for .. .. ..

 数字中的数字:
print(x [int(num)])

如果您使用 0 作为空格您必须在列表的开始位置添加空格

  x = [ '','a','b','c',...] 

x [0] ==''


I am new to python and I want to write a program that asks for a line of input (each number will be separated by a single space.) It would use a simple letter-number substitution cipher. Each letter will be appointed a number. So 1 = a, 2 = b and 3 = c until it reaches 26 = z. From there, however, the cipher would continue on so; 27 = a, 28 = b etc. 0's will be a space. The program will only use 0's and positive numbers. It would also print out the decryption of the message. For example:

Please type a code: 8 5 12 12 15

hello

and another example:

Please type a code: 16 25 20 8 14 0 9 19 0 3 15 15 12

python is cool

At the moment i have tried turning the alphabet into a list like this;

    n = int(input("Please type a code: ")
    x =['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']

...and then referring back to it later. However, im not exactly sure how this would work. I've also tried using the .replace() function. Like this:

    n = int(input("Please type a code: ")
    n = n.replace('1','a') #all the way until z and then loop it.
    print(n)

and so and and so on. But again, i have no idea how to do this properly. Any help would be greatly appreciated. Thanks.

解决方案

Use split()

numbers = input("Please type a code: ").split()

# ['16', '25', '20', '8', '14', '0', '9', ',19', '0', '3', '15', '15', '12']

Use for .. in ..

for num in numbers:
    print( x[int(num)] )

If you use 0 as space you have to add space at the begginig of list

x = [' ', 'a', 'b', 'c', .... ]

now x[0] == ' '

这篇关于在凯撒cypher在Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆