1
def message():
    answer1 = input('Welcome to the Caesar Cipher! What do you want to encrypt?')
    key = input('Enter the key number that you want to encrypt with. (1 - 26)')


def getMessage():
    while leng <= lengthOfInput1-1:
        lengthList.append(chr(ord(answer1[len]) + key))
        len += 1
    print(lengthList)


key = 0
answer1 = ''
maxKeySize = 26
lengthOfInput1 = len(answer1)
leng = 0
lengthList = []
message()
getMessage()

When I run this code, I always get the answer back as '[ ]'. I don't know what is going on and thought that the variable, 'lengthList' was a list and would append the letters. I also need help with having the individual letters go together to form an encrypted message. Thanks, Eric

Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49
eristic_
  • 13
  • 8
  • You only change `answer1` and `key` in the function. Those values are not related to `answer1` and `key` you defined in the outer scope. Use `return` to give back those values from the function. In the function you'll have `return (answer1, key)` and you'll have to call the function with `answer1, key = message()`. – Matthias Nov 25 '18 at 11:58
  • [Namespaces](https://stackoverflow.com/questions/3913217/what-are-python-namespaces-all-about)! – SuperShoot Nov 25 '18 at 12:13

2 Answers2

1

That's is happening because you have not assigned your answer1 variable in the outer. In the message() method you have assigned the values to variables local to it's scope, and not the answer1 variable in the outer scope as you wanted.

You would have to pass it as a parameter, and return it's value so that it retains it's value.

Similarly, getMessage() cannot access variables which are outside it's scope. So, you have to pass them as a parameter too!

A correct form of this code would look like:

def message():
    answer1 = input('Welcome to the Ceaser Cipher! What do you want to encrypt?')
    key = int(input('Enter the key number that you want to encrypt with. (1 - 26)'))
    return answer1, key


def getMessage(lengthOfInput1, lengthList, answer1, key, leng):
    while leng <= lengthOfInput1-1:
        lengthList.append(chr(ord(answer1[leng]) + key))
        leng += 1
    print(lengthList)


key = 0
answer1 = ''
answer1, key = message()
# Here I am returning both of the values from
# message method.

# LOCAL VARIABLES
maxKeySize = 26
lengthOfInput1 = len(answer1)
leng = 0
lengthList = []

getMessage(lengthOfInput1, lengthList, answer1, key, leng)
# Inorder to access the local variables, you will
# have to pass them to getMessage() method.

This above piece of code, will make a list lengthList which contains the letters of the cipher. However, it is local to getMessage(). You would have to return it to it's parent scope, to access it there.

Moreover, to make convert a list to a string, you would have to iterate within the list, and keep appending to an empty string. Something like this:

messageCoded = ""
for i in messageList:
    messageCoded += i

print(messageCoded)

Make sure you keep the scopes correct, and you will do good. This article explains in a well and good way.

MaJoR
  • 954
  • 7
  • 20
1

With minimal changes in your original code the working code is

def message():
    answer = input('Welcome to the Ceaser Cipher! \nWhat do you want to encrypt? ')
    key = input('Enter the key number that you want to encrypt with (1 - 26): ')
    return answer, int(key)


def getMessage(answer, key):
    lengthList = []
    leng = 0
    while leng < len(answer):
        lengthList.append(chr(ord(answer[leng]) + key))
        leng += 1
    print("".join(lengthList))


answer, key = message()
getMessage(answer, key)

The explanation:

  1. Your message() function fill the answer1 and key variables with user input but then throw them out, because these are only local variables - answer1 and key out of the definition of this function are others variables. So I included the return command to save them (and renamed answer1 to simply answer).

  2. The type of key variable in your message() function is str (string), but you need int. So the int(key) in the return statement.

  3. In the main code I saved those returned values - the answer, key = message() command.

  4. Your function getMessage() needs those values, so I added them as parameters to it - def getMessage(answer, key):

  5. I moved the commands

    lengthList = []
    leng = 0
    

    into the definition of the function getMessage().

  6. I change your while leng <= lengthOfInput1-1: command to while leng < len(answer): - note < instead of <=.

  7. You want to print as a result a string, not a list of individual characters - so I changed that list to a string with the "".join(lengthList) command. (The empty string "" is used as a delimiter between individual joined letters, so they will be joined without spaces or other symbols.)

  8. And I deleted all superfluous (useless) statements.

MarianD
  • 13,096
  • 12
  • 42
  • 54