0

I am trying to write a simple Rot13 Encoder/Decoder that takes a string and encodes/decodes it using the "codecs" module. I am trying to define a function using: codecs.encode('rot13_text', 'rot_13')

I have no problem using using the codecs module outside of a function. When I attempt to define a function using codecs.encode(rot13_text, 'rot_13') I receive a NameError

So far I have attempted many variations of the following code:

import codecs

def rot13_encoder():
    rot13_text = input("Type the text to be encoded: ")
    codecs.encode(rot13_text, 'rot_13')
    print(rot13_text)

Terminal Output

>>> def rot13_encoder():
...     rot13_text = input("Type the text to encode and press enter: ")
...     codecs.encode(rot13_text, 'rot_13')
...     print(rot13_text)
...
>>> rot13_encoder()
Type the text to encode and press enter: HELLO
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in rot13_encoder
  File "<string>", line 1, in <module>
NameError: name 'HELLO' is not defined
>>>   
8
  • There's no built-in rot_13 encoder/decoder. How have you registered your codec to begin with? Commented Sep 9, 2019 at 8:22
  • 1
    @AKX: There is a built-in rot_13 codec, at least in some versions of python. (also, hi :) Commented Sep 9, 2019 at 8:24
  • OP has tagged their question with python-3.x so I assume that's what they're using. (Hi!) Commented Sep 9, 2019 at 8:25
  • Your code works for me w/o any errors. I'm using python 3.7.3. Commented Sep 9, 2019 at 8:28
  • 1
    @AKX: It's present in both python-2.x and python-3.x; I'm not sure when it was introduced to 2.x, but it was apparently missing from 3.x versions before 3.2. Commented Sep 9, 2019 at 8:29

1 Answer 1

1

It seems you are using python 2.7 or earlier.

import codecs

def rot13_encoder(in_string):
    return codecs.encode(in_string, 'rot_13')

in_string = raw_input('Type the text to be encoded: ')
print(rot13_encoder(in_string))

in which case you should use raw_input(...) instead of input(...)

Sign up to request clarification or add additional context in comments.

1 Comment

I like the way you did this. Thank you very much for your correction. I appreciate your quick response. It's working just fine now! Thank you very much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.