Vigenère cipher:如何使加密消息出现在打印功能中?
Vigenère cipher: How to make it so that the encrypted message appears in a print function?
我是编码初学者,也是 Python。现在我正在研究 Vigenère cipher.
我已经可以使用密钥加密消息了。我在每段代码上添加了注释以供参考。这是我的代码。我的问题在代码下面。
# Ask user for message
print('type a message.')
message = input()
# print a white line for neatness
print()
# ask user for a key
print('give your key')
key = input()
# create a range with the length of the message
ran = range(len(message))
# Iterate though the range and therefor show all the letters
for i in ran:
# get current letters for this iteration
currentLetter = message[i]
currentKeyLetter = key[i % len(key)]
# Get corresponding numbers
numberLetter = ord(currentLetter)
numberKeyLetter = ord(currentKeyLetter)
# Add two letters
sumTwoLetters = numberLetter + numberKeyLetter
# Get the number of the encrypted letter
newNumberLetter = sumTwoLetters % 128
# Get the encrypted number based on number
newLetter = chr(newNumberLetter)
# print out the result
printText = currentLetter + "(" + str(numberLetter) + ") + "
printText += currentKeyLetter + "(" + str(numberKeyLetter) + ") = "
printText += newLetter + "(" + str(newNumberLetter) + ")"
print(printText)
该代码要求用户输入消息和密钥。 ran
变量创建一个包含消息长度的范围。
之后,for
循环使用 ord
和 chr
的密钥对消息进行加密
加密后的信件存放在变量newLetter
用户可以看到程序用 printText
做了什么
但是,我的问题是:如何使加密文本出现在单个字符串上。我试图循环进行。我失败得很惨(失败到不想表现出来)
有没有人对如何使加密消息显示在一行文本中有任何建议?
您可以在 list
中收集结果,然后 join()
在 for
循环结束后将它们全部合并到一个字符串中。查看更改的评论。
...
results = [] # ADDED.
# Iterate though the range and therefor show all the letters
for i in ran:
# get current letters for this iteration
currentLetter = message[i]
currentKeyLetter = key[i % len(key)]
# Get corresponding numbers
numberLetter = ord(currentLetter)
numberKeyLetter = ord(currentKeyLetter)
# Add two letters
sumTwoLetters = numberLetter + numberKeyLetter
# Get the number of the encrypted letter
newNumberLetter = sumTwoLetters % 128
# Get the encrypted number based on number
newLetter = chr(newNumberLetter)
# print out the result
printText = currentLetter + "(" + str(numberLetter) + ") + "
printText += currentKeyLetter + "(" + str(numberKeyLetter) + ") = "
printText += newLetter + "(" + str(newNumberLetter) + ")"
results.append(printText) # PUT IN results LIST INSTEAD OF PRINTING.
print(''.join(results)) # ADDED.
有两种简单的方法。 (1) 在循环内构建字符串并在循环外打印它,或者 (2) 在循环中移动时打印每个字符但没有换行符,在循环后加上一个换行符。
尝试其中一种方法,如果您需要更多帮助,请添加评论。您将通过自己尝试了解更多信息!
我是编码初学者,也是 Python。现在我正在研究 Vigenère cipher.
我已经可以使用密钥加密消息了。我在每段代码上添加了注释以供参考。这是我的代码。我的问题在代码下面。
# Ask user for message
print('type a message.')
message = input()
# print a white line for neatness
print()
# ask user for a key
print('give your key')
key = input()
# create a range with the length of the message
ran = range(len(message))
# Iterate though the range and therefor show all the letters
for i in ran:
# get current letters for this iteration
currentLetter = message[i]
currentKeyLetter = key[i % len(key)]
# Get corresponding numbers
numberLetter = ord(currentLetter)
numberKeyLetter = ord(currentKeyLetter)
# Add two letters
sumTwoLetters = numberLetter + numberKeyLetter
# Get the number of the encrypted letter
newNumberLetter = sumTwoLetters % 128
# Get the encrypted number based on number
newLetter = chr(newNumberLetter)
# print out the result
printText = currentLetter + "(" + str(numberLetter) + ") + "
printText += currentKeyLetter + "(" + str(numberKeyLetter) + ") = "
printText += newLetter + "(" + str(newNumberLetter) + ")"
print(printText)
该代码要求用户输入消息和密钥。 ran
变量创建一个包含消息长度的范围。
之后,for
循环使用 ord
和 chr
加密后的信件存放在变量newLetter
用户可以看到程序用 printText
但是,我的问题是:如何使加密文本出现在单个字符串上。我试图循环进行。我失败得很惨(失败到不想表现出来)
有没有人对如何使加密消息显示在一行文本中有任何建议?
您可以在 list
中收集结果,然后 join()
在 for
循环结束后将它们全部合并到一个字符串中。查看更改的评论。
...
results = [] # ADDED.
# Iterate though the range and therefor show all the letters
for i in ran:
# get current letters for this iteration
currentLetter = message[i]
currentKeyLetter = key[i % len(key)]
# Get corresponding numbers
numberLetter = ord(currentLetter)
numberKeyLetter = ord(currentKeyLetter)
# Add two letters
sumTwoLetters = numberLetter + numberKeyLetter
# Get the number of the encrypted letter
newNumberLetter = sumTwoLetters % 128
# Get the encrypted number based on number
newLetter = chr(newNumberLetter)
# print out the result
printText = currentLetter + "(" + str(numberLetter) + ") + "
printText += currentKeyLetter + "(" + str(numberKeyLetter) + ") = "
printText += newLetter + "(" + str(newNumberLetter) + ")"
results.append(printText) # PUT IN results LIST INSTEAD OF PRINTING.
print(''.join(results)) # ADDED.
有两种简单的方法。 (1) 在循环内构建字符串并在循环外打印它,或者 (2) 在循环中移动时打印每个字符但没有换行符,在循环后加上一个换行符。
尝试其中一种方法,如果您需要更多帮助,请添加评论。您将通过自己尝试了解更多信息!