0

Currently doing some school programming and I'm confused as to where I've gone wrong here:

#declaring array names.
longitude=[]; latitude=[]; messagetext=[];encryptions=[];
input_file = open('messages.txt', 'r')


lines_in_file_array = input_file.read().splitlines()
input_file.close()

#appending the lines in a select file to select records.
for line in lines_in_file_array:
     record_array = line.split(',')
     longitude.append(record_array[0])
     latitude.append(record_array[1])
     messagetext.append(record_array[2])


def encrypt():
    for index in range(len(messagetext)):
        x=messagetext[index]
        x=([ord(character)+2 for character in x])
        #the character under this seems to not like being defined. I'm      confused?
        codedx=str.join(chr(character),'','','','')
        encoded_text.append(codedx)
    print(codedx)


encrypt()
print(messagetext)

I keep getting the error that "character" is not defined. I have put internal commentary to help you identify the cause.

Thomas K
  • 39,200
  • 7
  • 84
  • 86
Alev
  • 19
  • 6
  • 5
    As the error is telling you `character` is not defined. Which is correct. If you look inside your `encrypt` method you are trying to use `character` without ever having declared it or assigning it any value. What should `character` be in your code? – idjaw Mar 16 '16 at 14:46
  • In addition to @idjaw's comment, in python we generally refer to arrays as lists. – Igor Mar 16 '16 at 14:50
  • @idjaw It's meant to be the ASCII for each character reasoning It's in a loop. I'm trying to turn each character in turn into ASCII, add two, and then back. I'm a little confused on how to do this now. – Alev Mar 16 '16 at 14:55
  • You create `x` in a list comprehension, and then don't use it. You need to apply `chr` in another comprehension. – hpaulj Mar 16 '16 at 16:18

2 Answers2

0
x=([ord(character)+2 for character in x])

character is local variable for list comprehension here. It would be undefined outside.

It's easy to see on some simple example:

[i for i in range(10)]
print(i)  # NameError: name 'i' is not defined
Mikhail Gerasimov
  • 36,989
  • 16
  • 116
  • 159
0

The problem you are having is about scope. The character variable only lives inside the pseudo for loop that you have crated inside the list comprehension. To fix this, you need 1) to access the characters inside X or 2) To create a for loop that will hold the existence of charachter

Example:

#declaring array names.
longitude=[]; latitude=[]; messagetext=[];encryptions=[];
input_file = open('messages.txt', 'r')


lines_in_file_array = input_file.read().splitlines()
input_file.close()

#appending the lines in a select file to select records.
for line in lines_in_file_array:
     record_array = line.split(',')
     longitude.append(record_array[0])
     latitude.append(record_array[1])
     messagetext.append(record_array[2])


def encrypt():
    for index in range(len(messagetext)):
        x=messagetext[index]
        for y in x:
            charachter =  ord(y)+2
            #the character under this seems to not like being defined. I'm      confused?
            codedx=str.join(chr(character),'','','','')
            encoded_text.append(codedx)
            print(codedx)


encrypt()
print(messagetext)
patito
  • 530
  • 2
  • 13