Python issue when reading from a file -
hello doing school project , wrote out got working next task make read message file changed definition asked message usermessage() run code works prints out whats in text file when last bit gets final answer shows error
traceback (most recent call last): file "c:\users\christian\desktop\python\2 keywor cypher code.py", line 138, in <module> restart() file "c:\users\christian\desktop\python\2 keywor cypher code.py", line 127, in restart text = translatemessage2(key2, message2, option) file "c:\users\christian\desktop\python\2 keywor cypher code.py", line 107, in translatemessage2 translated.append(symbol) # symbol not in letters, add translated is. nameerror: name 'translated' not defined import pyperclip valid_letters = 'zabcdefghijklmnopqrstuvwxy' # stores letters witch being used in program def linespacer(): print('================================================================') def getoption(): # first queston in program witch asking if want encrypt decrypt while true: print('do wish encrypt or decrypt message?') option = input().lower() if option in 'encrypt e decrypt d'.split(): return option # if user not enter right ifomation goes tot top , ask question else: print('enter either "encrypt" or "e" or "decrypt" or "d".') # if user doers not enter right thing example if enter 'l' prints message def usermessage(): mes = str(input('would encrypt or decrypt file or type own message?(file/f or message/m): ')) if mes == 'f' or mes == 'file': name = str(input("what document want read called?: ")) open(name, "rt") in_file: text = in_file.read() print(text) return text if mes == 'm' or mes == 'message': text = str(input('enter message: ')) def userkeyword(): # def ask user message , gathers infomation print('enter first keyword:') # prints out message asking keyword return input() def userkeyword2(): # def ask user message , gathers infomation print('enter second keyword:') # prints out message asking keyword return input() def translatemessage(key, message, option): # main def , of maths when call def reqires 3 variables translated = [] # stores encrypted/decrypted message string keyindex = 0 # defult keyindex when program started key = key.upper() # allowing user have upper case letters or lowercase letters symbol in message: # loop through each character in message num = valid_letters.find(symbol.upper()) # if num != -1: # -1 means symbol.upper() not found in letters if option == 'encrypt' or option == 'e': num += valid_letters.find(key[keyindex]) #this makes if encrypting adds elif option == 'decrypt' or option == 'd': num -= valid_letters.find(key[keyindex]) # makes if decrypting subtract num %= len(valid_letters) if symbol.isupper(): translated.append(valid_letters[num]) elif symbol.islower(): translated.append(valid_letters[num].lower()) keyindex += 1 # move next letter in key if keyindex == len(key): keyindex = 0 else: translated.append(symbol) # symbol not in letters, add translated is. return ''.join(translated) # joins of functions user can have of text def translatemessage2(key2, message2, option): # main def , of maths when call def reqires 3 variables translated2 = [] # stores encrypted/decrypted message string keyindex = 0 # defult keyindex when program started key2 = key2.upper() # allowing user have upper case letters or lowercase letters symbol in message2: # loop through each character in message num = valid_letters.find(symbol.upper()) # if num != -1: # -1 means symbol.upper() not found in letters if option == 'encrypt' or option == 'e': num += valid_letters.find(key2[keyindex]) #this makes if encrypting adds elif option == 'decrypt' or option == 'd': num -= valid_letters.find(key2[keyindex]) # makes if decrypting subtract num %= len(valid_letters) if symbol.isupper(): translated2.append(valid_letters[num]) elif symbol.islower(): translated2.append(valid_letters[num].lower()) keyindex += 1 # move next letter in key if keyindex == len(key2): keyindex = 0 else: translated.append(symbol) # symbol not in letters, add translated is. return ''.join(translated2) # joins of functions user can have of text def playagainmessage(): again = str(input("would restart y or n: ")); linespacer() if again == "y" or again == "y": # if statment saying if user types "y" runs code restart program restart(); # if user types "n" exits program elif again == "n" or again == "n": exit() def restart(): # def allows user restart prgram once have done option = getoption() message = usermessage() key = userkeyword() key2 = userkeyword2() message2 = translatemessage(key, message, option) text = translatemessage2(key2, message2, option) linespacer() print('your translated text is: %s' % text) # prints message , text has been encrypt or decrypt pyperclip.copy(text) linespacer() name = str(input("what name file: ")) name1 = str; file = open(name+".txt", "w") file.write(text) file.close() playagainmessage() restart()
you left translated in translatemessage2 when meant change translated2.
in translatemessage2:
translated.append(symbol)
should be:
translated2.append(symbol)
Comments
Post a Comment