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
#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


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

Possibly Related Threads…
Thread Author Replies Views Last Post
  NameError: name 'pi' is not defined katebishop 2 2,923 Jul-15-2024, 05:48 AM
Last Post: Pedroski55
  I'm getting a NameError: ...not defined. vonArre 2 6,063 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,808 Nov-23-2023, 02:53 PM
Last Post: rob101
  Printing the variable from defined function jws 7 8,489 Sep-03-2023, 03:22 PM
Last Post: deanhystad
  Getting NameError for a function that is defined JonWayn 2 2,267 Dec-11-2022, 01:53 PM
Last Post: JonWayn
  How to print the output of a defined function bshoushtarian 4 2,472 Sep-08-2022, 01:44 PM
Last Post: deanhystad
  [split] NameError: name 'csvwriter' is not defined. Did you mean: 'writer'? cathy12 4 5,954 Sep-01-2022, 07:41 PM
Last Post: deanhystad
  NameError: name ‘app_ctrl’ is not defined 3lnyn0 0 2,553 Jul-04-2022, 08:08 PM
Last Post: 3lnyn0
  User-defined function to reset variables? Mark17 3 3,497 May-25-2022, 07:22 PM
Last Post: Gribouillis
  NameError: name 'hash_value_x_t' is not defined Anldra12 5 3,272 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