Python Forum
Help with function - encryption - messages - NameError: name 'message' is not defined
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with function - encryption - messages - NameError: name 'message' is not defined
#1
Question 
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.
Reply
#2
encrypt
  • Has a rogue a on line 7
  • The parameter message has its passed-in attribute overwritten on line 6 by an empty string
  • When calling encrypt(message, shift) on line 19 neither message or shift have been defined

decrypt
  • The parameter message has its passed-in attribute overwritten on line 6 by an empty string
  • When calling decrypt(message,shift) on line 19 neither message or shift have been defined
Reply
#3
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 + c
But 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 += c
Avoid duplicate code when you can, even if it is as little as "encrypted_message = encrypted_message + "
Reply
#4
Question 
(Nov-09-2022, 06:36 PM)Yoriz Wrote: encrypt
  • Has a rogue a on line 7
  • The parameter message has its passed-in attribute overwritten on line 6 by an empty string
  • When calling encrypt(message, shift) on line 19 neither message or shift have been defined

decrypt
  • The parameter message has its passed-in attribute overwritten on line 6 by an empty string
  • When calling decrypt(message,shift) on line 19 neither message or shift have been defined

Yes, I do know that neither
Quote:message
or maybe
Quote:shift
hasn't been defined. How can I fix it please?
Reply
#5
(Nov-11-2022, 07:42 PM)MrKnd94 Wrote: [Yes, I do know that neither
Quote:message
or maybe
Quote:shift
hasn't been defined. How can I fix it please?
By defining them?

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}"')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I'm getting a NameError: ...not defined. vonArre 2 311 Mar-24-2024, 10:25 PM
Last Post: vonArre
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 602 Nov-23-2023, 02:53 PM
Last Post: rob101
  Printing the variable from defined function jws 7 1,335 Sep-03-2023, 03:22 PM
Last Post: deanhystad
  Getting NameError for a function that is defined JonWayn 2 1,122 Dec-11-2022, 01:53 PM
Last Post: JonWayn
  How to print the output of a defined function bshoushtarian 4 1,321 Sep-08-2022, 01:44 PM
Last Post: deanhystad
  [split] NameError: name 'csvwriter' is not defined. Did you mean: 'writer'? cathy12 4 3,352 Sep-01-2022, 07:41 PM
Last Post: deanhystad
  NameError: name ‘app_ctrl’ is not defined 3lnyn0 0 1,522 Jul-04-2022, 08:08 PM
Last Post: 3lnyn0
  User-defined function to reset variables? Mark17 3 1,661 May-25-2022, 07:22 PM
Last Post: Gribouillis
  NameError: name 'hash_value_x_t' is not defined Anldra12 5 1,935 May-13-2022, 03:37 PM
Last Post: deanhystad
  NameError: name 'cross_validation' is not defined tmhsa 6 13,374 Jan-17-2022, 09:53 PM
Last Post: TropicalHeat

Forum Jump:

User Panel Messages

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