Python中替换密码的问题
很抱歉代码太长,我已经尝试了一段时间来修复主函数中出现的错误。不知道从这里到哪里去Python中替换密码的问题,python,encryption,Python,Encryption,很抱歉代码太长,我已经尝试了一段时间来修复主函数中出现的错误。不知道从这里到哪里去 编辑以修复缩进问题,现在函数运行,但命令 “输入命令(getKey、changeKey、encrypt、decrypt、quit):”重复执行,而不考虑getKey以外的输入。当输入getKey时,出现错误“'SubstitutionCipher'对象没有属性'getKey'” 第二次编辑类替换密码允许函数至少为getKey提供一个输出 我不确定这些是语法、缩进、其他特性的问题,还是它们的组合问题。我很感谢你
- 编辑以修复缩进问题,现在函数运行,但命令 “输入命令(getKey、changeKey、encrypt、decrypt、quit):”重复执行,而不考虑getKey以外的输入。当输入getKey时,出现错误“'SubstitutionCipher'对象没有属性'getKey'”
- 第二次编辑类替换密码允许函数至少为getKey提供一个输出
我不确定这些是语法、缩进、其他特性的问题,还是它们的组合问题。我很感谢你的一些见解,因为我已经尝试了几天来解决这个问题。当前的问题是,中断在循环之外,当更改缩进时,我会收到一个错误,称为意外缩进。这是您的代码,缩进已修复,还有一些其他修复。这似乎奏效了
import random
#A global constant defining the alphabet
LCLETTERS = "abcdefghijklmnopqrstuvwxyz"
#function to check if given key is valid or not
def isLegalKey(key):
return ( len(key) == 26 and all ( [ch in key for ch in LCLETTERS ] ) )
#function to generate random key
def makeRandomKey():
lst = list( LCLETTERS )
random.shuffle( lst )
return ''.join( lst )
class SubstitutionCipher:
def __init__ (self, key = makeRandomKey() ):
self._key = key
def getKey( self ):
return self._key
def setKey( self, newKey ):
self._key = newKey
def encryptText( self, plaintext ):
alphabet = LCLETTERS
key = self._key
text = plaintext
result = ""
for letter in text:
if letter.lower() in alphabet:
result += key[alphabet.find(letter.lower())]
else:
result += letter
return result
def decryptText( self, ciphertext):
alphabet = LCLETTERS
key = self._key
text = ciphertext
result = ""
for letter in text:
if letter.lower() in key:
result += alphabet[key.find(letter.lower())]
else:
result += letter
return result
#main function
def main():
flag =0
cipherFun = SubstitutionCipher()
while flag ==0:
print("Enter a command (getKey, changeKey, encrypt, decrypt, quit): ")
str = input()
if str == 'getKey':
print(cipherFun.getKey())
elif str == 'changeKey':
while True:
print("Enter a valid cipher key, 'random' for a random key, or 'quit' to quit: ")
cipher = input()
if cipher == 'random':
cipherFun.setKey(makeRandomKey())
break
elif cipher == 'quit':
break
elif isLegalKey(cipher) == False:
print("Illegal key entered. Try again")
else:
cipherFun.setKey(cipher)
print("New cipher key: ", cipherFun.getKey())
break
elif str == 'encrypt':
print("Enter a text to encrypt: ")
str = input()
res = cipherFun.encryptText(str)
print("The encrypted text is: ", res)
elif str == 'decrypt':
print("Enter a text to decrypt: ")
str = input()
res = cipherFun.decryptText(str)
print("The decrypted text is: ", res)
elif str == 'quit':
print("Thanks for visiting!")
flag =1
else:
print("command not recognized. Try again")
main()
您看到了哪些错误,在哪些行中?将错误的完整回溯显示为问题中格式正确的文本。您最大的问题是以
if(str=='getKey'):
开头的每一行都需要缩进,以便它们是while
循环的一部分。另外请注意,Python中的if
语句不需要括号。验证密码的部分也需要缩进,以成为while
循环内部的一部分。您有巨大的缩进问题,至少如本文所示。替换密码中的成员函数也必须缩进。
import random
#A global constant defining the alphabet
LCLETTERS = "abcdefghijklmnopqrstuvwxyz"
#function to check if given key is valid or not
def isLegalKey(key):
return ( len(key) == 26 and all ( [ch in key for ch in LCLETTERS ] ) )
#function to generate random key
def makeRandomKey():
lst = list( LCLETTERS )
random.shuffle( lst )
return ''.join( lst )
class SubstitutionCipher:
def __init__ (self, key = makeRandomKey() ):
self._key = key
def getKey( self ):
return self._key
def setKey( self, newKey ):
self._key = newKey
def encryptText( self, plaintext ):
alphabet = LCLETTERS
key = self._key
text = plaintext
result = ""
for letter in text:
if letter.lower() in alphabet:
result += key[alphabet.find(letter.lower())]
else:
result += letter
return result
def decryptText( self, ciphertext):
alphabet = LCLETTERS
key = self._key
text = ciphertext
result = ""
for letter in text:
if letter.lower() in key:
result += alphabet[key.find(letter.lower())]
else:
result += letter
return result
#main function
def main():
flag =0
cipherFun = SubstitutionCipher()
while flag ==0:
print("Enter a command (getKey, changeKey, encrypt, decrypt, quit): ")
str = input()
if str == 'getKey':
print(cipherFun.getKey())
elif str == 'changeKey':
while True:
print("Enter a valid cipher key, 'random' for a random key, or 'quit' to quit: ")
cipher = input()
if cipher == 'random':
cipherFun.setKey(makeRandomKey())
break
elif cipher == 'quit':
break
elif isLegalKey(cipher) == False:
print("Illegal key entered. Try again")
else:
cipherFun.setKey(cipher)
print("New cipher key: ", cipherFun.getKey())
break
elif str == 'encrypt':
print("Enter a text to encrypt: ")
str = input()
res = cipherFun.encryptText(str)
print("The encrypted text is: ", res)
elif str == 'decrypt':
print("Enter a text to decrypt: ")
str = input()
res = cipherFun.decryptText(str)
print("The decrypted text is: ", res)
elif str == 'quit':
print("Thanks for visiting!")
flag =1
else:
print("command not recognized. Try again")
main()