Python Forum
Help with a misbehaving variable.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with a misbehaving variable.
#1
Hope this tag thing works.
So I had this program working as a stand alone but now I'm trying to turn it into reusable functions.
It keeps telling me I did not define output. I swear to GOD I have tried adding output = '' to just about every place I could even though I should not need too because I said what it = in the if statements. I copied a very similar program out of a book and it worked fine. Trying to rewrite it on my own for practice and I can't seem to get it to work. Can someone tell me why it won't accept this variable? I even used print to make sure it was registering the other variables and they seem to be fine.

Thanks
EDIT: Oh, and just to be cute I tried changing the name of that variable in all the places but that did not work either. It just changed the name of the variable it claims is not defined to that name.

#Column Transposition Cipher

import pyperclip, math

def main():

    mode = 'encrypt'
    message = """They look like big, good, strong hands. Don't they? I always thought that's what they were. My little friends. The little man with his racing snail, the Nighthob, even the stupid bat. I couldn't hold on to them. The nothing pulled them right out of my hands. I failed."""
    #message = """TssT  gIh. htc  e wehopfyDh euua oal llilntiNdllo' tineeotttg'ddk hlht . teet tlhy hhhie mooekywablme?en,d   r   rbIeweoii .ivngga te h,lMhntt wy  o ga ht ooylihtuosisehtd t  e ,ttrsmo hlat.fsoecu  tu ipTmrgfnihyohrgde nti   hg esbna tnnaonhhdattdaasi.hsnt.l i.d' ,In """
    key = 37
        
    if mode == 'encrypt':
        output = ''
        output = encryptTransposition(message, key)
    else:
        output = decryptTransposition(message, key)

print (output)
pyperclip.copy (output)


def encryptTransposition(message, key):
    columns = [''] * key
        
    for i in range(key):
        pointer = i
        while pointer < len(message):
            columns[i] += message[pointer]
            pointer += key
        columns = ''.join(columns)
    return (columns)
    

def decryptTransposition(message, key):
    
    numOfColumns = math.ceil(len(message) / key)
    numOfRows = key
    numOfUnused = (numOfColumns * numOfRows)-len(message)
    columns = [''] * numOfColumns

    x = 0
    y = 0

    for char in message:
        columns[x] += char
        x += 1
        if (x == numOfColumns) or (x == numOfColumns-1 and y >= numOfRows-numOfUnused):
            x = 0
            y += 1
        columns = ''.join(columns)
    return (columns)

if __name__ == '__main__':
    main()
Error:
Traceback (most recent call last): File "C:\Users\jarrod0987\AppData\Local\Programs\Python\Python36-32\columnTranspositionCipher2.py", line 18, in <module> print (output) NameError: name 'output' is not defined
Reply
#2
The first statement which is executed is print(output). As the variable is not defined, you get an error. If you want this statement and the following one to be part of main(), then indent them!
Reply
#3
check the indentation - I believe lines 18-19 are part of main function
Reply
#4
(Dec-16-2017, 07:28 PM)squenson Wrote: The first statement which is executed is print(output). As the variable is not defined, you get an error. If you want this statement and the following one to be part of main(), then indent them!

OK!!! :D
LOl thanks for that.
Had another error but I got everything working now. Thanks
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020