0

Hello I have a little problem over here. I am trying to create a Caesar style encryption/decryption. However the algorithm doesn't work that well. Can someone tell me where the problem is in the algorithm. I have tried, but I don't know what's wrong.

Here is what I have now:

    MAX_KEY_SIZE = 26

def getMode ():
    while True:
        print('Do you wish to encrypt or decrypt a message?')
        mode = input().lower()
        if mode in 'encrypt e decrypt d'.split():
            return mode
        else:
            print('Enter either "encrypt" or "e" or "decrypt" or "d".')

def getMessage ():
    print('Enter your message:')
    return input ()

def getKey():
    key = 0
    while True:
        print('Enter the key number (1-%s)' % (MAX_KEY_SIZE))
        key = int(input())
        if (key >= 1 and key <= MAX_KEY_SIZE):
            return key

def getTranslatedMessage(mode, message, key):
        if mode[0] == 'd':
            key = -key
        translated = ''

    for symbol in message:
        if symbol.isalpha():
            num = ord(symbol)
            num += key

            if symbol.isupper():
                if num > ord('Z'):
                    num -= 26
                elif num < ord('A'):
                    num += 26
            elif symbol.islower():
                if num > ord('z'):
                    num -= 26
                elif num < ord('a'):
                    num += 26

            translated += chr(num)
        else:
            translated += symbol
        return translated

mode = getMode()
message = getMessage()
key = getKey()

print ('Your translated text is: ')
print ('getTranslatedMessage(mode,message,key)')

THE ERROR I AM HAVING WHEN I AM TRYING TO SAY THAT I WANT TO ENCRYPT OR DECRYPT IS:

Traceback (most recent call last):
  File "C:\Python27\Enigma code.py", line 50, in <module>
    mode = getMode()
  File "C:\Python27\Enigma code.py", line 6, in getMode
    mode = input().lower()
  File "<string>", line 1, in <module>
NameError: name 'encrypt' is not defined
wwii
  • 23,232
  • 7
  • 37
  • 77
redouan
  • 5
  • 1
  • It's not homework so stop disliking thank you very much – redouan Sep 19 '17 at 18:55
  • 1
    The reason you're getting downvoted is because you failed to point out what error you're getting, and how your code isn't working. In fact, you should try to reduce the problem to the minimal amount of code necessary to demonstrate your problem. – TheCog19 Sep 19 '17 at 18:57
  • The error is: When I that I say that I want to encrypt. By typing encrypt this error pops up: Traceback (most recent call last): File "C:\Python27\Enigma code.py", line 50, in mode = getMode() File "C:\Python27\Enigma code.py", line 6, in getMode mode = input().lower() File "", line 1, in NameError: name 'encrypt' is not defined – redouan Sep 19 '17 at 19:00
  • Possible duplicate of [Python 2.7 getting user input and manipulating as string without quotations](https://stackoverflow.com/questions/4960208/python-2-7-getting-user-input-and-manipulating-as-string-without-quotations) – wwii Sep 19 '17 at 20:02

3 Answers3

0

You have an indentation error near for just below the getTranslatedMessage function. I tried indenting it and it ran perfectly fine.

Andrew Myers
  • 2,754
  • 5
  • 32
  • 40
prudhvi Indana
  • 789
  • 7
  • 19
0

The error that you point out in the comments is related to the input() behavior I think. When you run the python script in the shell and it reaches mode=getMode() you are in the interpreter, so writing : encrypt looks for a variable called encrypt and that's the reason you are getting that error.** edit writing "encrypt" with quotes would solve it or using the next modules

Probably you will like to know about the argv function in th sys module and the argparse module that both help you to provide the arguments at the start of the execution instead of stopping the execution in the middle

P.D. as previously said there's an indentation error already pointed out

Yfir Cross
  • 15
  • 5
0

For Python 2.7, input() tries to evaluate your response,

>>> x = input()
2+3
>>> x
5
>>> x = input()
foo

Traceback (most recent call last):
  File "<pyshell#14>", line 1, in <module>
    x = input()
  File "<string>", line 1, in <module>
NameError: name 'foo' is not defined

Use raw_input() instead.

>>> x = raw_input()
foo
>>> x
'foo'
>>> 

Using input in 2.7 is considered a security risk: your user, if knowledgeable, could enter something malicious to be evaluated. What's the difference between raw_input() and input() in python3.x?

wwii
  • 23,232
  • 7
  • 37
  • 77