![]() |
Help with function - encryption - messages - NameError: name 'message' is not defined - 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 function - encryption - messages - NameError: name 'message' is not defined (/thread-38657.html) |
Help with function - encryption - messages - NameError: name 'message' is not defined - MrKnd94 - Nov-09-2022 Hello! I'm trying to encrypt messages using the Caesar Cipher. I've made a function for it. This is the code for it: def encrypt(message,shift): alphabet = 'abcdefghijklmnopqrstuvwxyz' # The alphabet encrypted_message = "" # The encrypted message as an empty string message = "" a for c in message:# c travels through the message. if c in alphabet: # Checking to see if the message is in the alphabet first, finding position below. i = alphabet.index(c) # Where is c in the message? Counts the alphabet indexing at 0. j = (i + shift) % 26 # j can become greater (>) than 25, if we do "j = i + shift"; don't want that to happen, it won't work, so use % 2. encrypted_message = encrypted_message + alphabet[j] # Tells the program to add the characters to the end. else: # If it isn't apart of the alphabet, then... encrypted_message = encrypted_message + c # Adds a space print(encrypted_message) return '' encrypt(message, shift) Yeah. Long comments. I'm very sorry about that. And I'm also, in the same program, trying to decrypt the messages. This is the code for this (again, long comments, sorry): def decrypt(message,shift): alphabet = 'abcdefghijklmnopqrstuvwxyz' # The alphabet message = "" # The encrypted message as an empty string decrypted_message = "" for c in message:# c travels through the message. if c in alphabet: # Checking to see if the message is in the alphabet first, finding position below. i = alphabet.index(c) # Where is c in the message? Counts the alphabet indexing at 0. j = (26 + i - shift) % 26 # Won't go greater (>) 25; also, we might get negative numbers in the alphabet, we don't want that, so + 26. . . We % 26 because when "i" and "shift" become bigger than 26, it'll wrap aroun. decrypted_message = decrypted_message + alphabet[j] # Tells the program to add the characters to the end. else: # If it isn't apart of the alphabet, then... decrypted_message = decrypted_message + c # Adds a space print(decrypted_message) return '' decrypt(message,shift)And I've tested them out separately, and the error it is giving me is: Quote:NameError: name 'message' is not defined. I'm at a complete loss now. Any reason why it's giving me this error? Any help would be greatly appreciated. Thanks. RE: Help with function - encryption - messages - NameError: name 'message' is not defined - Yoriz - Nov-09-2022 encrypt
decrypt
RE: Help with function - encryption - messages - NameError: name 'message' is not defined - deanhystad - Nov-09-2022 Did you want to do something like this? shift = 10 message = encrypt(input("Message: "), shift) print(message() message = decrypt(message, shift) print(message)When you fix that error you will notice that encrypt and decrypt return empty strings. Inside the functions you are setting message="", so you are encrypting (or decrypting) an empty string regardless of what "message" you pass in. Why did you write a decrypt() function? Look at encrypt() and decrypt() see any similarities? Is there potential for code reuse? Finally, you can do this: if c in alphabet: i = alphabet.index(c) j = (i + shift) % 26 encrypted_message = encrypted_message + alphabet[j] else: encrypted_message = encrypted_message + cBut why when you can do this: # encrypt c if it is in alphabet, otherwise leave unchanged. if c in alphabet: c = alphabet[(alphabet.index(c) + shift) % len(alphabet)] encrypted_message += cAvoid duplicate code when you can, even if it is as little as "encrypted_message = encrypted_message + " RE: Help with function - encryption - messages - NameError: name 'message' is not defined - MrKnd94 - Nov-11-2022 (Nov-09-2022, 06:36 PM)Yoriz Wrote: Yes, I do know that neither Quote:messageor maybe Quote:shifthasn't been defined. How can I fix it please? RE: Help with function - encryption - messages - NameError: name 'message' is not defined - deanhystad - Nov-11-2022 (Nov-11-2022, 07:42 PM)MrKnd94 Wrote: [Yes, I do know that neitherBy defining them?Quote:messageor maybeQuote:shifthasn't been defined. How can I fix it please? The errors are in encrypt(message, shift)and decrypt(message,shift)For encrypting the message, how is your program supposed to accept input? Does the user type in a message? Are there test messages you are supposed to encrypt? Any of these would work: encrypt(input("Enter message: ", 8) encrypt("This is the message", 5) message = "This is the message" shift = 9 encrypt(message, shift)The same goes for decrypt. Instead of printing, your functions should return the modified string. That way you could print the encoded message to the console, or email the message, or have it printed in invisible ink. You could also pass the encrypted message as an argument to the decrypt() function to decrypt the encrypted message. shift = int(input("Enter shift: ") message = input("Enter Message: ") message = encrypt(message, shift) print(f'The encrypted message is "{message}"') message = decrypt(message, shift) print(f'The decrypted message is "{message}"') |