2016-12-03 64 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')