Python - Caeser Cipher Not Giving Right Output

D

dtran.ru

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)

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

Dave Angel

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

dtran.ru

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.

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

Steven D'Aprano

Thanks for your input Dave. Would the line be:

return numtochar(c1 + c2 %26)

Yes, that's the line that Dave is talking about.

The critical part is that expression "c1 + c2 %26" which gets calculated
before being passed on to numtochar. The % operator is a form of division
(it returns the remainder after division, so 12%5 returns 2) and like the
regular division operator / and multiplication * it has higher precedence
than addition.

That means that "30 + 40 % 26" calculates the % part first:

30 + 40 % 26
=> 30 + 14
=> 54

What you probably want is to calculate the % last, not first. That means
you need to perform the addition first. Use round brackets (parentheses)
for that:

(30 + 40) % 26
=> 70 % 26
=> 18


Does that help?
 
D

Dave Angel

Thanks for your input Dave. Would the line be:

return numtochar(c1 + c2 %26)

That would be the line I tagged, yes.
c1 and c2 are lower-case letters.

No, they're not , you just got done overwriting the letters with
numbers. That's a bad habit, reusing local variables with data
of different meanings, but I was trying to focus on the line
with the bug.

And I was wondering how I would add the partenthesis because I tried:

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

Please help us to help you by actually showing the traceback.
Doesn't matter in this case, but...

What order do you want the add and the modulo to happen? Use the
parentheses to say so. Or split it into two or more lines, with
another variable.

Perhaps you don't understand that % is an operator, just like +
and *. If you were told to add a and b before multiplying by c,
how would you write that? Try it, just as you tried the other
experiment I recommended.
 
R

Rustom Mody

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
 
D

dtran.ru

Yes, that's the line that Dave is talking about.



The critical part is that expression "c1 + c2 %26" which gets calculated

before being passed on to numtochar. The % operator is a form of division

(it returns the remainder after division, so 12%5 returns 2) and like the

regular division operator / and multiplication * it has higher precedence

than addition.



That means that "30 + 40 % 26" calculates the % part first:



30 + 40 % 26

=> 30 + 14

=> 54



What you probably want is to calculate the % last, not first. That means

you need to perform the addition first. Use round brackets (parentheses)

for that:



(30 + 40) % 26

=> 70 % 26

=> 18





Does that help?









--

Steven D'Aprano

http://import-that.dreamwidth.org/

Ooh I understand now! I've been coding for hours and exhaustion has gotten to my head. Many thanks all of you as I now know the source of my folly.
 
I

Ian Kelly

Please help us to help you by actually showing the traceback.
Doesn't matter in this case, but...

What order do you want the add and the modulo to happen? Use the
parentheses to say so.

Ah, but he did. x = y + z (mod m) is a perfectly well-formed math equation.
Not the OP's fault if it doesn't work that way in Python.
 
R

Rustom Mody

Ah, but he did. x = y + z (mod m) is a perfectly well-formed math equation. Not the OP's fault if it doesn't work that way in Python.

!!!!!
!I forgot that parse!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top