I am trying to do the following:
In an HTML TextArea, change the position of each character of the entered text in the ASCII table by 13 positions (in my code it is rot13 function).
Here’s the Rot13 function:
def rot13(s):
for a in s:
print chr(ord(a)+13)
It does work this way, but it prints as well header information and omits the last character. So when I enter the word ‘Hello’ in the box, the result is:
U r y y | Status: 200 Content-Type: text/html; charset=utf-8 Cache-Control: no-cache Content-Length: 4 None
So how to go about this?
Also, when I tried to do it this way:
def rot13(s):
for a in s:
chr(ord(a)+13)
return s
It returned the same text I’ve put in the text, without the changes I thought there would be. So as I understand, it doesn’t modify the ‘s’ directly that way? So how to go about this?
The whole code is below:
import webapp2
import cgi
def escape_html(s):
return cgi.escape(s, quote = True)
form = """<html>
<head><title>Rot13</title></head>
<body>
<form method="post">
Please enter your text here:<br>
<textarea name="text"></textarea>
<input type="submit">
</form>
</body>
</html>
"""
def rot13(s):
for a in s:
print chr(ord(a)+13)
class MainHandler(webapp2.RequestHandler):
def get(self):
self.response.out.write(form)
def post(self):
entered_text = self.request.get('text')
self.response.out.write(rot13(entered_text))
app = webapp2.WSGIApplication([
('/', MainHandler)
], debug=True)
EDIT: Here is a function for performing an actual ROT13 on a string (my apologies – I wasn’t aware this was an actual algorithm – the method I provided in the previous answer will not perform true ROT13). However if you are looking for an efficient way to implement it, you are going to have a hard time beating @NickJohnson’s answer 🙂 This translates according to table here, with uppercase mapping to uppercase and lowercase mapping to lowercase. This creates a mapping of uppercase and lowercase letters, mapped to their ROT13 equivalents by splitting the component parts in half and flipping them. You then zip the ‘normal’ and ‘reversed’ lists together and create a dictionary from the key/value pairs that are generated:
And to hopefully make it easier to visualize, here is what the key/value strings look like:
Instead of using
print, you’ll want to useself.response.out.write(as you have in other places). This will write the result to the HTTP response that is sent to the requester (note that headers are part of theresponseas well, but aren’t printed to the screen unless specified when usingself.response.out.write). In your case, a simplified version could look something like this:Instead of printing each character, it constructs a string using the function you defined. This will return that string, and the result will be written out as a
response. You could also add additional HTML formatting etc., but this should hopefully get you on the right track.Also, regarding why your second function didn’t work:
The reason that it returns
sunaltered is because although you are performing thechr(ord(a)+13)step, you aren’t doing anything with it. Your function is definitely on the right track, so one way to make it return identical output to the one in the example above is to create an empty list that will store each modified character, and thenjointhe list at the end: