Caeser Cipher Code,小写不起作用

时间:2018-05-23 20:21:56

标签: python-3.x

所以我试图制作一个包含加密的凯撒密码。到目前为止,我已经能够在这些方面取得成功,但我无法弄清楚我的代码如何使用较小的案例。例如,当您输入消息时(我们将使用密钥为1的HelLO),它将返回IFMMP。我遇到的问题是它不会将小写字母作为小写字母返回,而是作为大写字母返回。我想知道是否有人可以帮我解决我的问题。谢谢! PS:这是python3的代码     ABCs =

[ “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”, “A”, “b”, “C”, “d”, “E”, “F”, “G”, “H”, “I”, “J”,“K ”, “升”, “M”, “N”, “O”, “p”, “q”, “R”, “S”, “T”, “U”, “v”, “W”, “X”, “Y”, “Z”]

ans= input("Do you wish to encrypt, decrypt or brute force?")


if ans== "encrypt":
  msg=input("Enter your message:")
  key=int(input("Enter the key number (1-26)"))

  uncodedmsg= list(msg)
  for i in range (0, len(uncodedmsg)):
    letter=uncodedmsg[i]
    if letter  not in ABCs:
      i+=1
    else:
      letter2= ABCs.index(letter)
      letter3= letter2+key
      uncodedmsg[i]= ABCs[letter3%26] 
  messageend="".join(uncodedmsg)
  print ("Your translated text is:" + messageend )

1 个答案:

答案 0 :(得分:0)

您的问题在这里:

uncodedmsg[i] = ABCs[letter3 % 26]

你采取上限和下限的模数,所以你的答案永远不会超过26,所以你永远不会得到小写。

你可以做的是用这样的东西分支UpperCase和LowerCase:

ABCs = ["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"]
abcs = ["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"]

if letter.isupper():
    letter2 = ABCs.index(letter)
    letter3 = letter2 + key
    uncodedmsg[i] = ABCs[letter3 % 26]
if letter.islower():
    letter2 = abcs.index(letter)
    letter3 = letter2 + key
    uncodedmsg[i] = abcs[letter3 % 26]