Python - Caeser Cipher Not Giving Right Output

Rustom Mody rustompmody at gmail.com
Fri Mar 21 05:02:58 CET 2014


On Friday, March 21, 2014 8:53:49 AM UTC+5:30, wrote:
> On Thursday, March 20, 2014 11:16:50 PM UTC-4, Dave Angel wrote:
> > > Hello good people I am working on a caeser cipher program for class. However, I ran into a problem with my outputs. Up to a certain point for example:
> > > 1. two('y', 'z')
> > > Would give a '\x92' output instead of a 'x' output.
> > > Currently this is my code so far:
> > > def chartonum(ch):
> > >     return ord(ch) - 97 
> > > def numtochar(n):
> > >     return chr(n + 97) 
> > > def two(c1 , c2):
> > >     c1 = chartonum(c1)
> > >     c2 = chartonum(c2)
> > >     return numtochar(c1 + c2 %26)
> > You're missing some parentheses in that line. To test your
> >  understanding,  try picking some numbers for c1 and c2. Display
> >  c1 + c2 % 26, and see if the result is always between 0 and
> >  25.
> > Or look up the term precedence in your textbook.
> > > I am thinking I have messed up on my mod 26, however, I am at a lost where I might have went wrong in that. Any help would be appreciated.
> > -- 
> > DaveA

> Thanks for your input Dave. Would the line be:

> return numtochar(c1 + c2 %26) 

> c1 and c2 are lower-case letters. And I was wondering how I would add the partenthesis because I tried:

> return numtochar(c1 + c2 (%26)) and it gave me an error.

I suggest you put aside your assignment for 15 minutes. Then

1. Start up the python interpreter
2. Type ? + ?? % ???
   where the ?, ??, ??? take various values between 1 and 4
3. Try to put in parenthesis (ie '( )') here and there to modify your results
4. Then read the material on 'precedence' as Dave suggested



More information about the Python-list mailing list