2016-12-03 84 views
0

我不断收到以下错误:全球名称错误,即使它是

Traceback (most recent call last): 
    File "main.py", line 33, in <module> 
    main() 
    File "main.py", line 21, in main 
    translated = encrypt.encryptMess(mKey, content) 
    File "encrypt.py", line 7, in encryptMess 
    c = caesartranslate(content, mKey, myMode) 
NameError: name 'myMode' is not defined 

即使我已经在代码中已经定义myMode。我已经检查过我的缩进,并且所有的都是他们应该的。

import time, os, sys, encrypt, caesarCipher, reverseCipher, vigenereCipher, glob 

def main(): 
    inputFilename = 'frankensteinEnc.txt' 
    outputFilename = 'frankensteinEnc.encrypted.txt' 
    mKey = 5 
    myMode = 'encrypt' 

    if not os.path.exists(inputFilename): 
     print('The file %s does not exist. Exiting....' % (inputFilename)) 
     sys.exit() 

    fileObj = open(inputFilename) 
    content = fileObj.read() 
    fileObj.close() 

    print ('%sing...' % (myMode.title())) 

    startTime = time.time() 
    if myMode == 'encrypt': 
     translated = encrypt.encryptMess(mKey, content) 
    elif myMode == 'decrypt': 
     translated = decrypt.decryptMess(mKey, content) 

    outputFileObj = open(outputFilename, 'w') 
    outputFileObj.write(translated) 
    outputFileObj.close() 

    print('Done %sing %s (%s characters).' % (myMode, inputFilename, len(content))) 
    print('%sed file is %s.' % (myMode.title(), outputFilename)) 

if __name__ == '__main__': 
    main() 

我想要得到的代码使用凯撒密码,V @ genere加密加密文件和反向密码,但代码似乎被卡住这三个错误。请帮我

由于@be_good_do_good建议,我改变 翻译= encrypt.encryptMess(MKEY,内容) 这个 翻译= encrypt.encryptMess(MKEY,内容,myMode)

,这是我的加密的.py代码

from caesarCipher import * 
from reverseCipher import * 
from vigenereCipher import * 

def encryptMess (mKey, content, myMode): 
    c = caesartranslate(content, mKey, myMode) 
    print('Output from Caesar Cipher\t%s') %c 
    c1 = reverse(c) 
    print('Output from Reverse Cipher\t%s') % c1 
    c2 = vtranslate(c1,c, myMode) 
    return('Output from Vigenere Cipher\t%s') % c2 

在此之后,我得到这个错误回溯

Traceback (most recent call last): 
File "main.py", line 33, in <module> 
main() 
File "main.py", line 21, in main 
translated = encrypt.encryptMess(mKey, content, myMode) 
File "encrypt.py", line 7, in encryptMess 
print('Output from Caesar Cipher\t%s') %c 
TypeError: unsupported operand type(s) for %: 'NoneType' and 'str' 
+3

您的代码可能会缩进,但您的问题中的代码肯定不会显示,因此请先修复。 –

+1

还包括“encrypt.py”文件,它不是std lib –

+0

'myMode'是函数'main()'的局部变量,所以它不能从'encrypt.py'模块中的代码访问,除非它作为一个可调用的参数在它的内部。 Python中的“global”通常意味着_within_定义项目的模块,而不是模块之间的**。 – martineau

回答

0

您可能是没有经过myMode到encrypt.py

传如下:

translated = encrypt.encryptMess(mKey, content, myMode) 

和encrypt.py收到如下

def encryptMess(mKey, content, myMode): 

回溯您发布明确表示, encrypt.py没有定义myMode。至少将myMode设置为encrypt.py中某些默认值(如'encrypt'或'decrypt')