python 中的凯撒密码

发布于 2024-09-26 16:15:02 字数 1744 浏览 2 评论 0原文

我收到的错误是

Traceback (most recent call last):
  File "imp.py", line 52, in <module>
    mode = getMode()
  File "imp.py", line 8, in getMode
    mode = input().lower()
  File "<string>", line 1, in <module>
NameError: name 'encrypt' is not defined

下面的代码。

# Caesar Cipher


MAX_KEY_SIZE = 26

def getMode():
    while True:
        print('Do you wish to encrypt or decrypt a message?')
        mode = input().lower()
        if mode in 'encrypt e decrypt d'.split():
            return mode
        else:
            print('Enter either "encrypt" or "e" or "decrypt" or "d".')

def getMessage():
    print('Enter your message:')
    return input()

def getKey():
    key = 0
    while True:
        print('Enter the key number (1-%s)' % (MAX_KEY_SIZE))
        key = int(input())
        if (key >= 1 and key <= MAX_KEY_SIZE):
            return key

def getTranslatedMessage(mode, message, key):
    if mode[0] == 'd':
        key = -key
    translated = ''

    for symbol in message:
        if symbol.isalpha():
            num = ord(symbol)
            num += key

            if symbol.isupper():
                if num > ord('Z'):
                    num -= 26
                elif num < ord('A'):
                    num += 26
            elif symbol.islower():
                if num > ord('z'):
                    num -= 26
                elif num < ord('a'):
                    num += 26

            translated += chr(num)
        else:
            translated += symbol
    return translated

mode = getMode()
message = getMessage()
key = getKey()

print('Your translated text is:')
print(getTranslatedMessage(mode, message, key))

The error which i am getting is

Traceback (most recent call last):
  File "imp.py", line 52, in <module>
    mode = getMode()
  File "imp.py", line 8, in getMode
    mode = input().lower()
  File "<string>", line 1, in <module>
NameError: name 'encrypt' is not defined

Below is the code.

# Caesar Cipher


MAX_KEY_SIZE = 26

def getMode():
    while True:
        print('Do you wish to encrypt or decrypt a message?')
        mode = input().lower()
        if mode in 'encrypt e decrypt d'.split():
            return mode
        else:
            print('Enter either "encrypt" or "e" or "decrypt" or "d".')

def getMessage():
    print('Enter your message:')
    return input()

def getKey():
    key = 0
    while True:
        print('Enter the key number (1-%s)' % (MAX_KEY_SIZE))
        key = int(input())
        if (key >= 1 and key <= MAX_KEY_SIZE):
            return key

def getTranslatedMessage(mode, message, key):
    if mode[0] == 'd':
        key = -key
    translated = ''

    for symbol in message:
        if symbol.isalpha():
            num = ord(symbol)
            num += key

            if symbol.isupper():
                if num > ord('Z'):
                    num -= 26
                elif num < ord('A'):
                    num += 26
            elif symbol.islower():
                if num > ord('z'):
                    num -= 26
                elif num < ord('a'):
                    num += 26

            translated += chr(num)
        else:
            translated += symbol
    return translated

mode = getMode()
message = getMessage()
key = getKey()

print('Your translated text is:')
print(getTranslatedMessage(mode, message, key))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

债姬 2024-10-03 16:15:02

问题就在这里:

print('Do you wish to encrypt or decrypt a message?')
mode = input().lower()

在 Python 2.x 输入中使用 raw_input() 而不是 input()

Python 2.x:

  • 从标准输入读取字符串:raw_input()
  • 从标准输入读取字符串,然后对其求值:input()

Python 3.x:

  • 从标准输入读取字符串:input()
  • 从标准输入读取字符串,然后对其求值:eval(input())

The problem is here:

print('Do you wish to encrypt or decrypt a message?')
mode = input().lower()

In Python 2.x input use raw_input() instead of input().

Python 2.x:

  • Read a string from standard input: raw_input()
  • Read a string from standard input and then evaluate it: input().

Python 3.x:

  • Read a string from standard input: input()
  • Read a string from standard input and then evaluate it: eval(input()).
柳若烟 2024-10-03 16:15:02

input() 评估您键入的表达式。请改用 raw_input()

input() evaluates the expression you type. Use raw_input() instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文