Help with a misbehaving variable. - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Help with a misbehaving variable. (/thread-6995.html) |
Help with a misbehaving variable. - jarrod0987 - Dec-16-2017 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()
RE: Help with a misbehaving variable. - squenson - Dec-16-2017 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!
RE: Help with a misbehaving variable. - buran - Dec-16-2017 check the indentation - I believe lines 18-19 are part of main function
RE: Help with a misbehaving variable. - jarrod0987 - Dec-16-2017 (Dec-16-2017, 07:28 PM)squenson Wrote: The first statement which is executed is OK!!! :D LOl thanks for that. Had another error but I got everything working now. Thanks |