Reputation: 27
I have searched through threads on Stackoverflow and other sites using the Google machine and nothing gives me quite what I am looking for.
I am new programming student using Python 3 so most of my stuff is pretty basic.
I am writing a program that allows me to encrypt and decrypt a text file using the Caeser cipher. Taking a phrase from the user and applying a user given shift.
This is what I am working with right now:
Some of the code is just in there as a place holder until I get further along into my program.
import string
print ("1) Encrypt")
print ("2) Decrypt")
print ("3) Decrypt w/o Shift")
Choice = input("Choice: ")
if Choice == '1':
message = input("Message: ")
shift = int(input("Shift: "))
newmsg = ''
for char in message:
if char.isupper():
new = (ord(char)-ord('A')) + shift
newmsg = chr(new+ord('A'))
print (newmsg, end="")
else:
print(" ")
elif Choice == '2':
print ("2")
elif Choice == '3':
print ("3")
When I input a test phrase, such as "THIS IS A TEST", it gives me the output, with the correct encryption, but it displays it like this:
V
J
K
U
K
U
C
V
G
U
B
This is with a "shift" of 2
If I add end = ' '
to my print statement, it outputs as:
V J K U
K U
C
V G U V
If I add end = ''
to my print statement, it outputs as:
VJKU
KU
C
VGUV
I am looking for an output of:
VJKU KU C VGUV
I know this is something silly that I am overlooking. Any help would be appreciated. Thank you much.
Upvotes: 1
Views: 12111
Reputation: 601351
Use end=""
, but also add this parameter to the line
print(" ")
Otherwise, this line will add a newline.
That said, you are probably better off with first collecting the characters in a list, and calling print()
only once:
print("".join(list_of_characters))
Yet another approach would be to create a character translation table with str.maketrans()
and apply it with str.translate()
.
Upvotes: 5