您好我在这里遇到一些问题。我正在尝试创建一个凯撒风格的加密/解密。然而,该算法并不能很好地工作。有人能告诉我算法中的问题在哪里。我试过了,但我不知道出了什么问题。
以下是我现在所拥有的:
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)')
当我试图说出我想要加密或解密时所犯的错误:
Traceback (most recent call last):
File "C:\Python27\Enigma code.py", line 50, in <module>
mode = getMode()
File "C:\Python27\Enigma code.py", line 6, in getMode
mode = input().lower()
File "<string>", line 1, in <module>
NameError: name 'encrypt' is not defined
答案 0 :(得分:0)
getTranslatedMessage
函数正下方package me.jackboyplay.sockets_client;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author JackboyPlay
*/
public class Client {
public static void main(String[] args) throws IOException {
Socket socket = null;
try {
socket = new Socket("localhost", 5001);
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
if(socket == null){
return;
}
DataInputStream dis = null;
PrintWriter dos = null;
try {
dis = new DataInputStream(socket.getInputStream());
dos = new PrintWriter(socket.getOutputStream());
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
int x = 0;
Long l = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(5);
for(int i = 0; i < 50; i++){
dos.write("X: " + i);
}
}
}
附近有缩进错误。我尝试缩进它,它运行得非常好。
答案 1 :(得分:0)
您在评论中指出的错误与我认为的input()行为有关。当您在shell中运行python脚本并且它到达mode = getMode()时,您在解释器中,因此写入:encrypt会查找名为encrypt的变量,这就是您收到该错误的原因。**编辑写作&#34;加密&#34;引号会解决它或使用下一个模块
可能你想知道sys模块和argparse模块中的argv函数,它们都可以帮助你在执行开始时提供参数,而不是在中间停止执行
Pd积。如前所述,已经指出了缩进错误
答案 2 :(得分:0)
对于Python 2.7,input()
会尝试评估您的回复,
>>> x = input()
2+3
>>> x
5
>>> x = input()
foo
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
x = input()
File "<string>", line 1, in <module>
NameError: name 'foo' is not defined
改为使用raw_input()
。
>>> x = raw_input()
foo
>>> x
'foo'
>>>
在2.7中使用input
被认为是一种安全风险:如果您的用户知情,可能会输入恶意的内容进行评估。 What's the difference between raw_input() and input() in python3.x?