创建加解密程序
Create encryption and decryption program
我需要有人编辑我的代码来调试它。它不会显示任何加密或解密的文本,我认为这是因为格式混乱但我不知道。如果您能提供帮助,我们将不胜感激。 (我必须包括函数和用户输入)
result = ''
text = ''
text = input("Do you want to encrypt or decrypt the message?\n 1 to encrypt, 2 to decrypt or 0 to exit program. ")
def toList(text):
text.split()
return text
decrypted = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "
encrypted = b"qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM "
encrypt_table = bytes.maketrans(decrypted, encrypted)
decrypt_table = bytes.maketrans(encrypted, decrypted)
text = input('Enter message for encryption: ')
def encrypt(text):
result = ''
text = ''
result = text.translate(encrypt_table)
print(result + '\n\n')
cipherText = input('Enter message to decrypt: ')
def decrypt(cipherText):
result = ''
message = ''
result = message.translate(decrypt_table)
print(result + '\n\n')
if text == '1':
encrypt(text)
print(result + '\n\n')
elif text == '2':
decrypt(cipherText)
elif text != '0':
print('You have entered an invalid input, please try again. \n\n')
你有很多困惑。例如,看 encrypt
。你传入 text
,然后立即设置 text=''
,从而破坏输入的消息。同样,在decrypt
中,你传入cipherText
,但你运行message.translate
。你的函数需要 return 它们的结果,而不是打印它们。让调用者决定如何处理 returned 结果。
此外,将函数集中在模块的顶部是一个很好的做法,这样您就不会自欺欺人地认为调用顺序有误。
这是您的代码,经过修改后可以正常工作:
def encrypt(text):
result = text.translate(encrypt_table)
return result
def decrypt(message):
result = message.translate(decrypt_table)
return result
decrypted = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "
encrypted = b"qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM "
encrypt_table = bytes.maketrans(decrypted, encrypted)
decrypt_table = bytes.maketrans(encrypted, decrypted)
while True:
text = input("Do you want to encrypt or decrypt the message?\n 1 to encrypt, 2 to decrypt or 0 to exit program. ")
if text == '1':
text = input('Enter message for encryption: ')
result = encrypt(text)
print(result)
elif text == '2':
cipherText = input('Enter message to decrypt: ')
result = decrypt(cipherText)
print(result)
elif text == '0':
break
else:
print('You have entered an invalid input, please try again. \n\n')
请注意 encrypt
和 decrypt
并不真的需要存储在临时变量中。这样做的唯一原因是为了调试更方便,在 return
.
之前添加一个快速的 print(result)
我需要有人编辑我的代码来调试它。它不会显示任何加密或解密的文本,我认为这是因为格式混乱但我不知道。如果您能提供帮助,我们将不胜感激。 (我必须包括函数和用户输入)
result = ''
text = ''
text = input("Do you want to encrypt or decrypt the message?\n 1 to encrypt, 2 to decrypt or 0 to exit program. ")
def toList(text):
text.split()
return text
decrypted = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "
encrypted = b"qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM "
encrypt_table = bytes.maketrans(decrypted, encrypted)
decrypt_table = bytes.maketrans(encrypted, decrypted)
text = input('Enter message for encryption: ')
def encrypt(text):
result = ''
text = ''
result = text.translate(encrypt_table)
print(result + '\n\n')
cipherText = input('Enter message to decrypt: ')
def decrypt(cipherText):
result = ''
message = ''
result = message.translate(decrypt_table)
print(result + '\n\n')
if text == '1':
encrypt(text)
print(result + '\n\n')
elif text == '2':
decrypt(cipherText)
elif text != '0':
print('You have entered an invalid input, please try again. \n\n')
你有很多困惑。例如,看 encrypt
。你传入 text
,然后立即设置 text=''
,从而破坏输入的消息。同样,在decrypt
中,你传入cipherText
,但你运行message.translate
。你的函数需要 return 它们的结果,而不是打印它们。让调用者决定如何处理 returned 结果。
此外,将函数集中在模块的顶部是一个很好的做法,这样您就不会自欺欺人地认为调用顺序有误。
这是您的代码,经过修改后可以正常工作:
def encrypt(text):
result = text.translate(encrypt_table)
return result
def decrypt(message):
result = message.translate(decrypt_table)
return result
decrypted = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "
encrypted = b"qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM "
encrypt_table = bytes.maketrans(decrypted, encrypted)
decrypt_table = bytes.maketrans(encrypted, decrypted)
while True:
text = input("Do you want to encrypt or decrypt the message?\n 1 to encrypt, 2 to decrypt or 0 to exit program. ")
if text == '1':
text = input('Enter message for encryption: ')
result = encrypt(text)
print(result)
elif text == '2':
cipherText = input('Enter message to decrypt: ')
result = decrypt(cipherText)
print(result)
elif text == '0':
break
else:
print('You have entered an invalid input, please try again. \n\n')
请注意 encrypt
和 decrypt
并不真的需要存储在临时变量中。这样做的唯一原因是为了调试更方便,在 return
.
print(result)