2
\$\begingroup\$

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")
reader= csv.reader(ifile)

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

for row in reader:
    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?

\$\endgroup\$
4
  • \$\begingroup\$ What do you mean by An encryption sub procedure? \$\endgroup\$ Commented Feb 28, 2018 at 11:31
  • \$\begingroup\$ @Raimund Krämer sub procedure ≈ function \$\endgroup\$
    – Daniel
    Commented Feb 28, 2018 at 14:45
  • \$\begingroup\$ @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. \$\endgroup\$ Commented Feb 28, 2018 at 16:31
  • \$\begingroup\$ @Raimund Krämer Perhaps it's yet to be implemented, who knows. \$\endgroup\$
    – Daniel
    Commented Feb 28, 2018 at 19:26

2 Answers 2

1
\$\begingroup\$

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.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Note that end defaults to '\n'. \$\endgroup\$
    – Daniel
    Commented Feb 28, 2018 at 14:48
  • \$\begingroup\$ Did you mean end='\n\n'? \$\endgroup\$ Commented Feb 28, 2018 at 14:50
  • \$\begingroup\$ Thanks coal, i'll edit the answer. And @Solomon why would I mean that? \$\endgroup\$
    – Tony
    Commented Feb 28, 2018 at 14:55
  • 2
    \$\begingroup\$ print "Hello World!" would output Hello World!\n. print "Hello World!\n" would output Hello World!\n\n. \$\endgroup\$ Commented Feb 28, 2018 at 14:57
  • \$\begingroup\$ I see, havn't used python 2.x in awhile didn't realize it defaults a newline. Thanks! \$\endgroup\$
    – Tony
    Commented Feb 28, 2018 at 14:59
0
\$\begingroup\$
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)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.