By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,264 Members | 1,277 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,264 IT Pros & Developers. It's quick & easy.

Python - Caeser Cipher Not Giving Right Output

Yozuru
P: 4
Hello good people, I am able to run my code and it works fine.

However I did a test of:

Expand|Select|Wrap|Line Numbers
  1. two('y', 'z')
and received a '\x92' output instead of a 'x' output.

This is my code so far:

Expand|Select|Wrap|Line Numbers
  1. def chartonum(ch):
  2.     return ord(ch) - 97 
  3.  
  4. def numtochar(n):
  5.     return chr(n + 97) 
  6.  
  7. def two(c1 , c2):
  8.     c1 = chartonum(c1)
  9.     c2 = chartonum(c2)
  10.     return numtochar(c1 + c2 %26)
  11.  
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.

EDIT: SOLVED.

Line 10 error in precedence. so ((c1 + c1)%26)
Mar 21 '14 #1
Share this Question
Share on Google+
1 Reply


Yozuru
P: 4
I have found my answer, it was in a problem in line 10 with precedence. Such a simple error that have cost me a few hours. Hope this is useful to those who will see this.
Mar 21 '14 #2

Post your reply

Sign in to post your reply or Sign up for a free account.