Sam.Natale Sam.Natale - 2 months ago 26
Python Question

Caesar Cipher Code Python printing on separate lines

The code below works fine, however the message prints onto separate lines once it has been encrypted. For example if I type: abc with the shift of 1 it encrypts it but prints it back as:

b
c
d


And I don't understand why. I want it to print as:

bcd


Here is the code:

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))

Answer

Indentation matters and you shouldn't create new list of NewMessage everytime

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))
Comments