凯撒密码程序正在运行,但现在不起作用(请帮忙)- Python 3

Caesar Cipher program was working but now isn't(help please) - Python 3

我正在为受控评估的一部分编写凯撒密码代码。我构建了一个功能齐全的程序,我以为我有问题,但是在改变了一些东西之后我回去检查,一切都出错了!

代码很不整洁,但我现在有点厌倦了编码,所以我已经上网征求别人的意见。

代码:

answer ="C"
while answer == "C":

    lettersList=['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','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']

    def menu():
        userChoice=input("Would you like to encrypt or decrypt a message? E or D.\n").lower()
        while userChoice != "e" and userChoice != "d":
            print("Invalid.")
            userChoice=input("Would you like to encrypt or decrypt a message? E or D.\n").lower()
        print("\n")
        return userChoice

    def getPlaintext():
        plaintext= input("Please enter the message you would like encrypted/decrypted\n").lower()
        while plaintext.isalpha() == False:
            print("Invalid")
            plaintext=input("Please enter the message you would like encrypted/decrypted\n").lower()
        print("\n")
        return plaintext

    def getKey():
        key=int(input("Please enter a key. 1-26\n"))
        while key > 26 or key < 1:
            print("Invalid.")
            key=int(input("Please enter a key. 1-26\n"))
        print("\n")
        return key


    def encryptText(plaintext,key):
        characterNumber = 0
        newMessage = ""
        for characters in plaintext:
            character = plaintext[characterNumber]
            characterPosition = lettersList.index(character)
            newPosition=character+key
            newLetter = lettersList[newPosition]
            newMessage = (newMessage+newLetter)
            characterNumber= characterNumber+1
        print(newMessage)

    def decryptText(plaintext,key):
        characterNumber = 0
        newMessage = ""
        for characters in plaintext:
            character = plaintext[characterNumber]
            characterPosition = lettersList.index(character)
            print(characterPosition)
            newPosition=characterPosition-key
            newLetter = lettersList[newPosition]
            newMessage = (newMessage+newLetter)
            characterNumber= characterNumber+1
            newMessage = (newMessage.lower())
        print(newMessage)

    userChoice=menu()
    plaintext=getPlaintext()
    key=getKey()
    if userChoice == "e":
        encryptText(plaintext,key)
    elif userChoice == "d":
        decryptText(plaintext,key)
    print(newMessage)

在decypryptText中,newPosition可以为负数。在这种情况下,您应该环绕整个字母表。最好的方法是使用 % 运算符:

newPosition = (characterPosition - key) % len(lettersList)

顺便说一句,在 encryptText 中使用它也可以让您只制作字母表的一个副本,而不是您使用的两个副本。

这段代码中还有其他可以完善的东西,但没有这一个重要。

编辑:如果你想让 decryptText 大写,你可以使 decryptText 只是 return newMessage.upper()。同理可以使encryptText return newMessage.lower().

您的代码中存在一些简单的拼写错误,导致无法正常运行。 for characters in plaintext: 应该是 for character in plaintext: - 在您的代码中您创建了一个新变量 characters ,它没有在任何地方使用。 带有注释的正确加密函数如下所示:

def encryptText(plaintext, key):
    newMessage = ""
    for character in plaintext:  # 'character' holds each letter from 'plaintext'
        characterPosition = lettersList.index(character)
        newPosition = characterPosition + key  # shift the index, not the character!
        newLetter = lettersList[newPosition]
        newMessage = newMessage + newLetter  # append new letter to new message

    print(newMessage)

为了在 lettersList 上正常工作,每个字符只出现一次(即长度为 26),您需要检查移位索引是否超出范围;即字符不大于26,如果是,则减去26,像这样

newPosition = characterPosition + key
if newPosition >= len(lettersList) then:
    newPosition -= len(lettersList)

或按照建议使用模运算符。 这应该让您了解如何将解密函数也修改为 "wrap around"。