-2

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))
Sam.Natale
  • 50
  • 1
  • 1
  • 9

3 Answers3

1

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))
Tomasz Plaskota
  • 1,329
  • 12
  • 23
  • That works thanks! Can you maybe explain the commands you used as I am not familiar. (College Student) – Sam.Natale Sep 23 '16 at 11:27
  • Only command used here is `append`. What it does it appends element to list. Read more in documentation: https://docs.python.org/3/tutorial/datastructures.html#more-on-lists – Tomasz Plaskota Sep 23 '16 at 11:32
  • What about NewMessageList = [] Does that just assign the variable to nothing? – Sam.Natale Sep 23 '16 at 11:35
  • not nothing. [] is empty list, () is empty tuple, {} is empty dictonary, set() is empty set, list() is empty list and so on... – Tomasz Plaskota Sep 23 '16 at 11:36
  • I still don't get what append actually does. – Sam.Natale Sep 23 '16 at 11:38
  • Appends passed item. In docs: `Add an item to the end of the list; equivalent to a[len(a):] = [x].` Plenty of clean explanations on SO: http://stackoverflow.com/questions/252703/append-vs-extend – Tomasz Plaskota Sep 23 '16 at 11:40
0

you should change the following part;

print("".join(NewMessageList), end="")
Mustafa DOGRU
  • 3,994
  • 1
  • 16
  • 24
0

What happening was is that for each charachter it was running the loop and printing the answer, now I have collected all the encrypted letter and clubbed them as one in the end and printed it.

it at first initialize an empty list with NewMessage = [] and then for every letter that we get encrypted it adds to that empty list using .append() and at end print all by ''.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))
harshil9968
  • 3,254
  • 1
  • 16
  • 26
  • What does NewMessage = [] do and .append? – Sam.Natale Sep 23 '16 at 11:32
  • it at first initialize an empty list with `NewMessage = []` and then for every letter that we get encrypted it adds to that empty list using `.append()` and at end print all by `''.join(NewMessage)` – harshil9968 Sep 23 '16 at 11:34