R45PUT1N
R45PUT1N

Reputation: 1

Trouble defining a function using the "codecs" module

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

Upvotes: 0

Views: 214

Answers (1)

user5386938
user5386938

Reputation:

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(...)

Upvotes: 1

Related Questions