Python 程序未返回预期结果
因此,我编写了一个程序,使用Caesar shift算法对消息进行加密。代码如下:Python 程序未返回预期结果,python,Python,因此,我编写了一个程序,使用Caesar shift算法对消息进行加密。代码如下: 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(): ret
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 value you want your message to be shifted by. You can choose anything from 1 to 26.')
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))
MAX\u KEY\u SIZE=26
def getMode():
尽管如此:
print('是否要加密或解密邮件?')
模式=输入()
如果模式为“加密”“解密d”“.split():
返回模式
其他:
打印('输入“encrypt”或“e”或“decrypt”或“d”。)
def getMessage():
打印('输入您的消息:')
返回输入()
def getKey():
键=0
尽管如此:
print('输入您希望消息移位的值。您可以选择1到26之间的任何值')
key=int(输入())
如果(键>=1和键ord('Z'):
num-=26
elif numord('z'):
num-=26
elif num
下面是我在Visual Studio代码中运行它时发生的情况:
“是否要加密或解密邮件?
加密
输入您的消息:
银币
输入您希望邮件移动的值。您可以选择1到26之间的任意值。
三,
你的译文是:
“没有”
问题是,无论为encrpytion输入了什么消息,我都会得到相同的答案“无”,而不是加密消息
有人知道我该如何解决这个问题吗?我相信你的问题完全在你的缩进中,你最初的if语句中应该有一些部分没有 使用python时,必须非常小心缩进,因为没有控制条件的括号
MAX\u KEY\u SIZE=26
def getMode():
尽管如此:
print('是否要加密或解密邮件?')
模式=输入()
如果模式为“加密”“解密d”“.split():
返回模式
其他:
打印('输入“encrypt”或“e”或“decrypt”或“d”。)
def getMessage():
打印('输入您的消息:')
返回输入()
def getKey():
键=0
尽管如此:
print('输入您希望消息移位的值。您可以选择1到26之间的任何值')
key=int(输入())
如果(键>=1和键ord('Z'):
num-=26
elif numord('z'):
num-=26
elif num
在调用最后一次打印之前,您能快速调试并打印模式、消息和键吗?您想查看模式、消息和键的内容吗?我想了解输入到翻译功能的内容,是的。我知道您接受用户输入,但定义基本情况并检查getTranslatedMessage的输入内容代码>据我所知,它会向用户请求输入,获取def getTranslatedMessage(模式、消息、键):然后不打印任何内容作为答案。执行此操作:print(模式)、print(消息)、print(键)
在最后一次打印之前谢谢您的回答!解决了问题。