Need help with caesar cipher

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Mar 2007
Posts: 10
Reputation: pyguy25 is an unknown quantity at this point 
Solved Threads: 0
pyguy25 pyguy25 is offline Offline
Newbie Poster

Need help with caesar cipher

 
0
  #1
Mar 7th, 2007
Hello. I was wondering if anyone could help me with a caesar cipher program I am attempting to create. I was asked to write a caesar cipher encoder program. Ok. No problem. This is what i got:
  1. import string
  2. def main():
  3. print "This program will encode your messages using a Caesar Cipher"
  4. print
  5. key = input("Enter the key: ")
  6. message = raw_input("Enter the message: ")
  7. codedMessage = ""
  8. for ch in message:
  9. codedMessage = codedMessage + chr(ord(ch) + key)
  10. print "The coded message is:", codedMessage
  11.  
  12. main()
This works fine. But then I am asked to modify it to make it circular, where after "z" it will return to "a". I am at a loss for how to do this. This is probably a stupid question, but I am a beginner to Python and desperately need assistance. Please help. Thanks.
Last edited by pyguy25; Mar 7th, 2007 at 7:24 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: Need help with caesar cipher

 
0
  #2
Mar 7th, 2007
That's a good start. I might do something like

  1. ...
  2. for ch in message:
  3. code_val = ord(ch) + key
  4. if code_val > ord('z'):
  5. code_val -= 26 # better: ord('z') - ord('a'), just in case len(alphabet) != 26
  6. codedMessage = codedMessage+chr(code_val)
  7. ...

Jeff
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 10
Reputation: pyguy25 is an unknown quantity at this point 
Solved Threads: 0
pyguy25 pyguy25 is offline Offline
Newbie Poster

Re: Need help with caesar cipher

 
0
  #3
Mar 7th, 2007
Thanks. That really helps. Would there also be a way for me to make it so non-alphabetic characters (spaces, punctuation, etc.) do not change with the rest of the text?
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 133
Reputation: mawe is an unknown quantity at this point 
Solved Threads: 58
mawe mawe is offline Offline
Junior Poster

Re: Need help with caesar cipher

 
0
  #4
Mar 8th, 2007
  1. ...
  2. for char in message:
  3. # check if the character is alphabetic
  4. if char.isalpha():
  5. ...

Regards, mawe
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 1,551
Reputation: Lardmeister is an unknown quantity at this point 
Solved Threads: 23
Lardmeister's Avatar
Lardmeister Lardmeister is offline Offline
Posting Virtuoso

Re: Need help with caesar cipher

 
0
  #5
Mar 8th, 2007
Sounds interesting, so i put it all together:
  1. message = "just a simple test!"
  2. key = 11
  3. coded_message = ""
  4. for ch in message:
  5. code_val = ord(ch) + key
  6. if ch.isalpha():
  7. if code_val > ord('z'):
  8. code_val -= ord('z') - ord('a')
  9. coded_message = coded_message + chr(code_val)
  10. else:
  11. coded_message = coded_message + ch
  12. print message
  13. print coded_message
-- and I get this:
  1. just a simple test!
  2. ugef l etxbwp fpef!
-- great, but how can I get the original message back?
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: Need help with caesar cipher

 
0
  #6
Mar 8th, 2007
Well, what you're trying to do is invert the function that got you there to begin with. See if you can decode a couple of examples by hand and then go from there.

Jeff
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 10
Reputation: pyguy25 is an unknown quantity at this point 
Solved Threads: 0
pyguy25 pyguy25 is offline Offline
Newbie Poster

Re: Need help with caesar cipher

 
0
  #7
Mar 9th, 2007
Thanks so much everyone! That makes the program look so much better. I think I may be starting to understand this a little better.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC