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


Messages In This Thread
Help with function - encryption - messages - NameError: name 'message' is not defined - by MrKnd94 - Nov-09-2022, 05:28 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  NameError: name 'pi' is not defined katebishop 2 2,875 Jul-15-2024, 05:48 AM
Last Post: Pedroski55
  I'm getting a NameError: ...not defined. vonArre 2 5,899 Mar-24-2024, 10:25 PM
Last Post: vonArre
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 1,778 Nov-23-2023, 02:53 PM
Last Post: rob101
  Printing the variable from defined function jws 7 8,370 Sep-03-2023, 03:22 PM
Last Post: deanhystad
  Getting NameError for a function that is defined JonWayn 2 2,238 Dec-11-2022, 01:53 PM
Last Post: JonWayn
  How to print the output of a defined function bshoushtarian 4 2,429 Sep-08-2022, 01:44 PM
Last Post: deanhystad
  [split] NameError: name 'csvwriter' is not defined. Did you mean: 'writer'? cathy12 4 5,917 Sep-01-2022, 07:41 PM
Last Post: deanhystad
  NameError: name ‘app_ctrl’ is not defined 3lnyn0 0 2,531 Jul-04-2022, 08:08 PM
Last Post: 3lnyn0
  User-defined function to reset variables? Mark17 3 3,457 May-25-2022, 07:22 PM
Last Post: Gribouillis
  NameError: name 'hash_value_x_t' is not defined Anldra12 5 3,240 May-13-2022, 03:37 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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