[Tutor] implementing rot 13 problems

Jos Kerc joskerc at gmail.com
Tue Mar 12 07:21:37 CET 2013


On Tue, Mar 12, 2013 at 6:57 AM, RJ Ewing <ewing.rj at gmail.com> wrote:

> I am trying to implement rot 13 and am having troubles. My code is as
> follows:
>
> class Rot_13(webapp2.RequestHandler):
> def write_form(self, user=""):
>  self.response.out.write(rot_form % user)
>  def get(self):
> self.write_form()
>  def post(self):
>  user = self.request.get("text")
> s = self.rot_text(user)
>  print s
> s = "".join(s)
>  self.escape_html(s)
> self.write_form(s)
>

You might want to read up on the translate() method.

>  def rot_text(self, s):
>  ls = [i for i in s]
> for i in ls:
>  if i.isalpha():
> if i.isupper():
>  if i <= 'M':
> x = ls.index(i)
>  ls[ls.index(i)] = chr(ord(i) + 13)
> else:
>  x = ls.index(i)
> ls[ls.index(i)] = chr(ord(i) - 13)
>  elif i.islower():
>  if i <= 'm':
> x = ls.index(i)
>  ls[x] = chr(ord(i) + 13)
> elif i > 'm':
>  x = ls.index(i)
> ls[x] = chr(ord(i) - 13)
>  return ls
>
>  def escape_html(self, s):
> return cgi.escape(s, quote = True)
>
> Now if I enter abc, I get nop. But if I enter abcdefghijklmnop, I
> get abcqrstuvwxyznop. I have included print statements to check if ls[x] is
> changing and it is, but something is going wrong when I return the ls, and
> I am not quite sure what it is.
>
> Me neither, how are you calling /returning it?

> Thanks
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130312/99f25e58/attachment-0001.html>


More information about the Tutor mailing list