问题在于,当我运行我的代码时,它给了我5个加密文本,但我只想要它一次。它在五个不同的行中打印相同的加密文本五次。我该如何解决这个问题?
这是我的代码:
import string
encryptedtext = ""
currentpos = 0
plainText = input("Please enter your message: ")
offset = int(input('enter offset (Negative number to decrypt)'))
plainTextLen = len(plainText)
for x in plainText:
alpha = string.ascii_lowercase
plainText = plainText.lower()
position = alpha.find(plainText[0])
position += 5
encryptedText= ""
encryptedText += alpha[position]
encrypted_alpha = (alpha[alpha.index(alpha[offset]):len(alpha)])+ alpha[0:offset]
data = str.maketrans(alpha,encrypted_alpha)
final_message = str.translate(plainText, data)
print(final_message)
print("",encryptedText,"")
答案 0 :(得分:0)
此代码:
for x in plainText:
alpha = string.ascii_lowercase
plainText = plainText.lower()
position = alpha.find(plainText[0])
position += 5
encryptedText= ""
encryptedText += alpha[position]
encrypted_alpha = (alpha[alpha.index(alpha[offset]):len(alpha)])+ alpha[0:offset]
data = str.maketrans(alpha,encrypted_alpha)
final_message = str.translate(plainText, data)
print(final_message)
使用变量plainText
循环遍历x
的内容。但是你没有使用x
,所以你只做了N次完全相同的事情(包括打印)。
取出循环,然后运行一次代码。
# for x in plainText: <- removed
alpha = string.ascii_lowercase
plainText = plainText.lower()
position = alpha.find(plainText[0])
position += 5
encryptedText= ""
encryptedText += alpha[position]
encrypted_alpha = (alpha[alpha.index(alpha[offset]):len(alpha)])+ alpha[0:offset]
data = str.maketrans(alpha,encrypted_alpha)
final_message = str.translate(plainText, data)
print(final_message)