Joshua Lankamp
Joshua Lankamp

Reputation: 15

Program not returning expected results

So I've written a program to encrypt a message using the Caesar shift algorithm. Here is the code:

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 value you want your message to be shifted by. You can choose anything from 1 to 26.')
        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))

Here is what happens when I run it in Visual Studio Code:

"Do you wish to encrypt or decrypt a message? encrypt

Enter your message: silver

Enter the value you want your message to be shifted by. You can choose anything from 1 to 26. 3

Your translated text is: None"

The problem is regardless of what message is entered for encrpytion, I always get the same answer of 'none' instead of the encrypted message.

Does anyone have any idea on how I can fix this?

Upvotes: 0

Views: 45

Answers (1)

big_bad_bison
big_bad_bison

Reputation: 1015

I believe your issue is completely in your indenting, there were sections that were supposed to be in your initial if statement that were not.

With python you have to be very careful with indenting since there are no brackets that control the conditionals.

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 value you want your message to be shifted by. You can choose anything from 1 to 26.')
        key = int(input())
        if (key >= 1 and key <= MAX_KEY_SIZE):
            return key

def getTranslatedMessage(mode, message, key):
    translated = ''

    if mode[0] == 'd':
        key = -key

    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))

Upvotes: 1

Related Questions