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'
您的代碼可能會縮進,但您的問題中的代碼肯定不會顯示,因此請先修復。 –
還包括「encrypt.py」文件,它不是std lib –
'myMode'是函數'main()'的局部變量,所以它不能從'encrypt.py'模塊中的代碼訪問,除非它作爲一個可調用的參數在它的內部。 Python中的「global」通常意味着_within_定義項目的模塊,而不是模塊之間的**。 – martineau