# ASCII encryption program in Python

I have recently written a program that reads information from a CSV file and puts it into lists. An encryption sub procedure converts the characters to ASCII, adds 2 then converts them back to characters.

import csv

ifile = open("messages.csv","rb")

plain_text= []
plain_ascii= []
encrypted_ascii= []
encrypted_text= []
latitude= []
longitude= []

latitude.append(row[0])
longitude.append(row[1])
plain_text.append(row[2])

encrypted_text=[''.join(chr(ord(ch)+2) for ch in string) for string in
plain_text]
print plain_text
print encrypted_text

count=0

print ""
while True:
print ""
response=int(raw_input("Enter 1 to see the next record, 2 to see the
previous record or 3 to end the program:"))
print""
if response > 3 or response < 1:
print ("Please respond with one of the set responses.")
elif response == 3:
print "Process ended"
break
else:
if response==1:
count += 1
if count >= len(plain_text):
count = 0
elif response==2:
count -= 1
if count < 0:
count = len(plain_text) - 1
print ""
print("Record {0}:".format(count))
print ""
print("Latitude: {0}  Longitude: {1}  Unencrypted Text: {2}
Encrypted Text: {3}".format(latitude[count],longitude[count],
plain_text[count], encrypted_text[count]))

ifile.close()


Is there any way to make the entirety of this much more precise or simpler, and possibly any way to restructure this?

• What do you mean by An encryption sub procedure? Feb 28 '18 at 11:31
• @Raimund Krämer sub procedure ≈ function Feb 28 '18 at 14:45
• @Coal_ I know what a sub procedure is, I just don't see one defined in the code. By asking what OP means I meant what he refers to. My guess is the part encrypted_text=[''.join(chr(ord(ch)+2) for ch in string) for string in plain_text], but that is a list comprehension. Feb 28 '18 at 16:31
• @Raimund Krämer Perhaps it's yet to be implemented, who knows. Feb 28 '18 at 19:26

Just a few notes about print, I see you're not using parenthesis so i'm guessing you're using python 2.x. I would urge you to switch to python 3.5+ now, as python 2.x is at end of life. However if you insist on using it

-Keep consistent on using parenthesis

-You can use \n inside the string to add a newline:

print "Hello World!\n"


If you switch to python 3.x you can use the print function and pass in the end parameter to append a new line after your string:

s = "Hello World!"
print(s, end='\n') #NOTE: @Coal_ points out this is the default
# so if you didn't want there to be a newline you'd have to overwrite
# print(s, end=' ')
# @Solomon, and if you wanted a space between your outputs,
# print(s, end='\n\n')


That way you don't have seemingly useless print statements everywhere.

• Note that end defaults to '\n'. Feb 28 '18 at 14:48
• Did you mean end='\n\n'? Feb 28 '18 at 14:50
• Thanks coal, i'll edit the answer. And @Solomon why would I mean that?
– Tony
Feb 28 '18 at 14:55
• print "Hello World!" would output Hello World!\n. print "Hello World!\n" would output Hello World!\n\n. Feb 28 '18 at 14:57
• I see, havn't used python 2.x in awhile didn't realize it defaults a newline. Thanks!
– Tony
Feb 28 '18 at 14:59
response=int(raw_input("Enter 1 to see the next record, 2 to see the previous record or 3 to end the program:"))
print""
if response > 3 or response < 1:


When you're checking for failure, instead of asking "is it one of these cases that I've thought of that are bad", ask "Is it not one of the case that are good". Doing the former raises the issue of whether you've thought of everything that can go wrong. What if they enter a float? A string? Instead, do:

response = (raw_input("Enter 1 to see the next record, 2 to see the previous record or 3 to end the program:"))
if response not in ['1','2','3']:


You're not using integer properties of response; you're just checking whether it's equal to a comparison value, not doing math on it, so there's no need to convert to int. If you are going to convert, do it after you've checked whether the input is valid.

You can use modular arithematic to get rid of some if statements:

if response=='1':
count += 1
elif response=='2':
count -= 1
count = count%len(plaint_text)