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.
answer1andkeyin the function. Those values are not related toanswer1andkeyyou defined in the outer scope. Usereturnto give back those values from the function. In the function you'll havereturn (answer1, key)and you'll have to call the function withanswer1, key = message().