Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #68659 > unrolled thread

Python - Caeser Cipher Not Giving Right Output

Started bydtran.ru@gmail.com
First post2014-03-20 19:50 -0700
Last post2014-03-22 10:29 -0700
Articles 10 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  Python - Caeser Cipher Not Giving Right Output dtran.ru@gmail.com - 2014-03-20 19:50 -0700
    Re:Python - Caeser Cipher Not Giving Right Output Dave Angel <davea@davea.name> - 2014-03-20 23:16 -0400
      Re: Python - Caeser Cipher Not Giving Right Output dtran.ru@gmail.com - 2014-03-20 20:23 -0700
        Re: Python - Caeser Cipher Not Giving Right Output Dave Angel <davea@davea.name> - 2014-03-21 00:02 -0400
        Re: Python - Caeser Cipher Not Giving Right Output Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-21 03:58 +0000
          Re: Python - Caeser Cipher Not Giving Right Output dtran.ru@gmail.com - 2014-03-20 21:23 -0700
            Re: Python - Caeser Cipher Not Giving Right Output Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-03-21 09:50 +0000
        Re: Python - Caeser Cipher Not Giving Right Output Rustom Mody <rustompmody@gmail.com> - 2014-03-20 21:02 -0700
        Re: Python - Caeser Cipher Not Giving Right Output Ian Kelly <ian.g.kelly@gmail.com> - 2014-03-22 08:22 -0600
          Re: Python - Caeser Cipher Not Giving Right Output Rustom Mody <rustompmody@gmail.com> - 2014-03-22 10:29 -0700

#68659 — Python - Caeser Cipher Not Giving Right Output

Fromdtran.ru@gmail.com
Date2014-03-20 19:50 -0700
SubjectPython - Caeser Cipher Not Giving Right Output
Message-ID<7eee0f2b-0d5f-409e-ae4e-8c9c1af31d74@googlegroups.com>
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.

[toc] | [next] | [standalone]


#68664

FromDave Angel <davea@davea.name>
Date2014-03-20 23:16 -0400
Message-ID<mailman.8341.1395371543.18130.python-list@python.org>
In reply to#68659
 dtran.ru@gmail.com Wrote in message:
> 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

[toc] | [prev] | [next] | [standalone]


#68666

Fromdtran.ru@gmail.com
Date2014-03-20 20:23 -0700
Message-ID<bda05813-d186-41ea-a40d-5d36f90a8fed@googlegroups.com>
In reply to#68664
On Thursday, March 20, 2014 11:16:50 PM UTC-4, Dave Angel wrote:
> dtran.ru@gmail.com Wrote in message:
> 
> > 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. 

[toc] | [prev] | [next] | [standalone]


#68668

FromDave Angel <davea@davea.name>
Date2014-03-21 00:02 -0400
Message-ID<mailman.8343.1395374292.18130.python-list@python.org>
In reply to#68666
 dtran.ru@gmail.com Wrote in message:
> On Thursday, March 20, 2014 11:16:50 PM UTC-4, Dave Angel wrote:
>> dtran.ru@gmail.com Wrote in message:
>> > 
>> 
>> > 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.
>> 
>
>> DaveA
> 
> 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. 




-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#68669

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-03-21 03:58 +0000
Message-ID<532bb8f3$0$29994$c3e8da3$5496439d@news.astraweb.com>
In reply to#68666
On Thu, 20 Mar 2014 20:23:49 -0700, dtran.ru wrote:

> 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?




-- 
Steven D'Aprano
http://import-that.dreamwidth.org/

[toc] | [prev] | [next] | [standalone]


#68671

Fromdtran.ru@gmail.com
Date2014-03-20 21:23 -0700
Message-ID<32c74575-6387-45d5-ab8b-b414c035c98a@googlegroups.com>
In reply to#68669
On Thursday, March 20, 2014 11:58:43 PM UTC-4, Steven D'Aprano wrote:
> On Thu, 20 Mar 2014 20:23:49 -0700, dtran.ru wrote:
> 
> 
> 
> > 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?
> 
> 
> 
> 
> 
> 
> 
> 
> 
> -- 
> 
> 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. 

[toc] | [prev] | [next] | [standalone]


#68682

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2014-03-21 09:50 +0000
Message-ID<mailman.8350.1395395443.18130.python-list@python.org>
In reply to#68671
On 21/03/2014 04:23, dtran.ru@gmail.com wrote:

Would you please access this list via 
https://mail.python.org/mailman/listinfo/python-list or read and action 
this https://wiki.python.org/moin/GoogleGroupsPython to prevent us 
seeing double line spacing and single line paragraphs, thanks.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com

[toc] | [prev] | [next] | [standalone]


#68670

FromRustom Mody <rustompmody@gmail.com>
Date2014-03-20 21:02 -0700
Message-ID<22bdaf7b-cace-46d8-a0d8-785fd5d83e74@googlegroups.com>
In reply to#68666
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

[toc] | [prev] | [next] | [standalone]


#68777

FromIan Kelly <ian.g.kelly@gmail.com>
Date2014-03-22 08:22 -0600
Message-ID<mailman.8400.1395498157.18130.python-list@python.org>
In reply to#68666

[Multipart message — attachments visible in raw view] — view raw

On Mar 20, 2014 9:59 PM, "Dave Angel" <davea@davea.name> wrote:
>
>  dtran.ru@gmail.com Wrote in message:
> > 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.

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.

[toc] | [prev] | [next] | [standalone]


#68783

FromRustom Mody <rustompmody@gmail.com>
Date2014-03-22 10:29 -0700
Message-ID<3ddffa42-6f51-4950-94bd-560b9c9547b1@googlegroups.com>
In reply to#68777
On Saturday, March 22, 2014 7:52:28 PM UTC+5:30, Ian wrote:
> On Mar 20, 2014 9:59 PM, "Dave Angel" <da...@davea.name> wrote:
> >  dtra...@gmail.com Wrote in message:
> > > 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.
> 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!

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web