Caesar Cipher Code Python在单独的行上打印

时间:2016-09-23 11:21:19

标签: python encryption caesar-cipher

下面的代码运行正常,但是一旦加密,邮件就会打印到单独的行上。例如,如果我键入:abc,其移位为1,则会对其进行加密,但会将其打印为:

b
c
d

我不明白为什么。我希望它打印为:

 bcd 

以下是代码:

print("Welcome to the Code-Breaking/Making Software")
print("This program uses something called a Caesar Cipher.")

Message = (input("Please enter the message you wish to Encrypt >> "))
Shift = int(input("Please enter the shift for your message >> "))

for x in Message:
    OrdMessage = ord(x) 
    ShiftedMessage = OrdMessage + Shift
    NewMessage = chr(ShiftedMessage)
    NewMessageList = list(NewMessage)
    print("".join(NewMessageList))

3 个答案:

答案 0 :(得分:1)

缩进很重要,您不应该每次都创建NewMessage的新列表

print("Welcome to the Code-Breaking/Making Software")
print("This program uses something called a Caesar Cipher.")

Message = (input("Please enter the message you wish to Encrypt >> "))
Shift = int(input("Please enter the shift for your message >> "))

NewMessageList = []
for x in Message:
    OrdMessage = ord(x) 
    ShiftedMessage = OrdMessage + Shift
    NewMessage = chr(ShiftedMessage)
    NewMessageList.append(NewMessage)
print("".join(NewMessageList))

答案 1 :(得分:0)

你应该改变以下部分;

print("".join(NewMessageList), end="")

答案 2 :(得分:0)

发生的事情是,对于每个charachter,它正在运行循环并打印答案,现在我收集了所有加密的字母并最终将它们打成一个并打印出来。

首先使用NewMessage = []初始化一个空列表,然后对于我们加密的每个字母,它使用.append()添加到该空列表,最后打印全部按''.join(NewMessage)

print("Welcome to the Code-Breaking/Making Software")
print("This program uses something called a Caesar Cipher.")

Message = (input("Please enter the message you wish to Encrypt >> "))
Shift = int(input("Please enter the shift for your message >> "))

NewMessage = []
for x in Message:
    OrdMessage = ord(x) 
    ShiftedMessage = OrdMessage + Shift
    NewMessage.append(chr(ShiftedMessage)) 
print(''.join(NewMessage))