-1

For example, this code outputs:

Encrypted message: J 6 5 + ~ 5 + ~ = ~ * 9 + *

Decrypted message: T ~ h ~ i ~ s ~ ~ i ~ s ~ ~ a ~ ~ t ~ e ~ s ~ t

...due to the fact that whitespace is considered '~' by the decryption method.

from collections import defaultdict

e1 = {}
e2 = {}
e3 = {} 
d3 = {}
d2 = {}
d1 = {}

print "Would you like to encrypt 'e' or decrypt 'd' a message?"
count = 1
while count >= 1:
    eod = raw_input("> ")
    if eod in ("Encrypt", "encrypt", "e", "E"):
        print "Enter your message here:"
        emessage_in = raw_input("> ")
        elength = len(emessage_in)
        elistm = list(emessage_in)
        for x in range(0, elength):
            e1[x] = elistm[x]
            e2[x] = ord(e1[x])
            e3[x] = 158 - e2[x]
            emessage_out = chr(e3[x])
            print emessage_out,
            count = 0
    elif eod in ("Decrypt", "dencrypt", "d", "D"):
        print "Enter your message here:"
        dmessage_in = raw_input("> ")
        dlength = len(dmessage_in)
        dlistm = list(dmessage_in)
        for y in range(0, dlength):
            d3[y] = dlistm[y]
            d2[y] = ord(d3[y])
            d1[y] = 158 - d2[y]
            dmessage_out = chr(d1[y])
            print dmessage_out,
            count = 0
    else:
        print "Please type 'e' or 'd':"
        count += 1
Artjom B.
  • 61,146
  • 24
  • 125
  • 222

1 Answers1

0

Would you believe that it took me ten minutes of troubleshooting to realize that you were calling print in a loop? :) Added/modified lines are commented.

if eod in ("Encrypt", "encrypt", "e", "E"):
    print "Enter your message here:"
    emessage_in = raw_input("> ")
    elength = len(emessage_in)
    elistm = list(emessage_in)
    emessage_out = '' # initialize an empty string
    for x in range(0, elength):
        e1[x] = elistm[x]
        e2[x] = ord(e1[x])
        e3[x] = 158 - e2[x]
        emessage_out += chr(e3[x]) # concatenate each character onto that string
        count = 0
    print emessage_out # move this out of the loop
elif eod in ("Decrypt", "dencrypt", "d", "D"):
    print "Enter your message here:"
    dmessage_in = raw_input("> ")
    dlength = len(dmessage_in)
    dlistm = list(dmessage_in)
    dmessage_out = '' # initialize an empty string
    for y in range(0, dlength):
        d3[y] = dlistm[y]
        d2[y] = ord(d3[y])
        d1[y] = 158 - d2[y]
        dmessage_out += chr(d1[y]) # concatenate each character onto that string
        count = 0
    print dmessage_out # move this out of the loop
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
  • I already tried this, it works in the console but not when I add it to the code! – Jack Sellers Mar 31 '15 at 22:29
  • As it turns out, you didn't need to remove whitespace so much as avoid printing it in the first place. See the edited answer. – TigerhawkT3 Mar 31 '15 at 23:10
  • Thanks a lot for your taking the time to run my code (even if it was just 10 minutes)!!! I really appreciate it. I only started learning Python a few weeks ago so sometimes I run into problems that I can't solve by simply looking at other people's similar issues. – Jack Sellers Apr 01 '15 at 00:09