0
    #!/usr/bin/python

    password_out = ''
    case_changer = ord('a') - ord('A')
    encryption_key = (('a','m'), ('b','h'), ('c','t'), ('d','f'), ('e','g'),
    ('f','k'), ('g','b'), ('h','p'), ('i','j'), ('j','w'), ('k','e'), ('l','r'),
    ('m','q'), ('n','s'), ('o','l'), ('p','n'), ('q','i'), ('r','u'), ('s','o'),
    ('t','x'), ('u','z'), ('v','y'), ('w','v'), ('x','d'), ('y','c'), ('z','a'))

    #program greeting
    print ('This program will encrypt and decrypt user passwords \n')

    # get selection (encrypt /decrypt)
    which = input('Enter (e) to encrypt a password, and (d) to decrypt :')



     while which != 'e' and which != 'd':
            which = input ("\nINVALID -Enter 'e' to encrypt, 'd' to decrypt:")

        encrypting = (which == 'e') # assigns True or False

        # get password
        password_in = input ('Enter password:')

        #perform encryption / decryption

if encrypting:
    from_index = 0
    to_index = 1
else:
    from_index = 1
    to_index = 0

    case_changer = ord('a') - ord('A')

    for ch in password_in:
        letter_found = False

        for t in encryption_key:
            if ('a' <= ch and ch <= 'z') and ch == t[from_index]:
                password_out = password_out + t[to_index]
                letter_found = True

            elif ('A' <= ch and ch <= 'Z') and chr(ord(ch) + 32) == t[from_index]:
                password_out = password_out + chr(ord(t[to_index]) - case_changer)
                letter_found = True

        if not letter_found:
            password_out = password_out + ch

    # output
    if encrypting:
        print ("Your encrypted password is : ", password_out)
    else:
        print ("Your decrypted password is : ", password_out)

question: This code is from the book Introduction to computer science. I didn't quite get the ord() function, only 'a' to 'A' is defined. How does it subtract value from others? I checked the python documentation for ord() function but didn't quite answer my question.

sunp
  • 21
  • 6

1 Answers1

-2

EDIT: You should use raw_input() function to avoid that Python tries to evaluate expresion in input.

acostela
  • 2,597
  • 3
  • 33
  • 50
  • 1
    Parentheses within string literals have no special meaning. – jonrsharpe Nov 13 '14 at 08:23
  • raw_input works! Thanks. – sunp Nov 13 '14 at 08:24
  • I didn't understand the use of ord() function: "case_changer = ord('a') - ord('A')". Can you put some light on it. I've checked the documentation but that doesn't answer my question. – sunp Nov 13 '14 at 08:29
  • @jonrsharpe "In Python 2, raw_input() returns a string, and input() tries to run the input as a Python expression." If python 3 is used "eval(input())" Is another solution. – acostela Nov 13 '14 at 08:32
  • @acostela I am well aware of that, my comment addresses the first half of the answer. – jonrsharpe Nov 13 '14 at 08:33
  • @sunnyp. what exactly *is* your question? How did the documentation fail to answer it? What have you tried (e.g. In the interpreter) to answer your own question? – jonrsharpe Nov 13 '14 at 08:34
  • @jonrsharpe I edited my commment I noticed that I wrote it wrong sorry – acostela Nov 13 '14 at 08:36
  • @acostela `eval(input())` is **not** a solution to this problem, it's a way to get 2.x's `input` in 3.x, where `input` is equivalent to 2.x's `raw_input`. The OP: 1. Isn't using 3.x; and 2. Is having problems precisely *because* of the `eval` step - if they were using 3.x (as the code is written for) they wouldn't have a problem. – jonrsharpe Nov 13 '14 at 08:42
  • @jonsharpe I tried it on the interpreter for specific values of 'a' and 'A' and it shows the values respectively but my question is different. I want to know how all the user input values are changed using : ord('a') - ord('A') because it only subtracts value of A from a. What about other alphabets. How does it work, is it something like a range. – sunp Nov 13 '14 at 09:02
  • @jonsharpe even if it is case_changer = ord('b') - ord('Z') or case_changer = ord('a') - ord('A') it still gives the same values. How? both encrypt 'sunny' as 'ozssc'. No difference? I hope my question is more clear now. – sunp Nov 13 '14 at 09:07
  • @sunnyp. Look at an ASCII chart; `(ord('a') - ord('A')) == (ord('b') - ord('B')) == (ord('z') - ord('Z'))`. – jonrsharpe Nov 13 '14 at 09:08
  • @jonsharpe I got your point, right now I am also doing it on interpreter. Your logic is right but I changed it in the script from ord('b') - ord('Z') it still gives the same values. Shouldn't it give different values?... values means password here. – sunp Nov 13 '14 at 09:17
  • @sunnyp. I would definitely expect some changes in output, but note that `case_changer` isn't applied in every case. Perhaps you should take some time to figure out exactly what the code does, line by line? – jonrsharpe Nov 13 '14 at 09:27