944,028 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 15905
  • Python RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 7th, 2007
0

Need help with caesar cipher

Expand Post »
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:
Python Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pyguy25 is offline Offline
10 posts
since Mar 2007
Mar 7th, 2007
0

Re: Need help with caesar cipher

That's a good start. I might do something like

Python Syntax (Toggle Plain Text)
  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
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Mar 7th, 2007
0

Re: Need help with caesar cipher

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pyguy25 is offline Offline
10 posts
since Mar 2007
Mar 8th, 2007
0

Re: Need help with caesar cipher

Python Syntax (Toggle Plain Text)
  1. ...
  2. for char in message:
  3. # check if the character is alphabetic
  4. if char.isalpha():
  5. ...

Regards, mawe
Reputation Points: 19
Solved Threads: 58
Junior Poster
mawe is offline Offline
133 posts
since Sep 2005
Mar 8th, 2007
0

Re: Need help with caesar cipher

Sounds interesting, so i put it all together:
Python Syntax (Toggle Plain Text)
  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:
Python Syntax (Toggle Plain Text)
  1. just a simple test!
  2. ugef l etxbwp fpef!
-- great, but how can I get the original message back?
Reputation Points: 407
Solved Threads: 36
Posting Virtuoso
Lardmeister is offline Offline
1,701 posts
since Mar 2007
Mar 8th, 2007
0

Re: Need help with caesar cipher

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
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Mar 9th, 2007
0

Re: Need help with caesar cipher

Thanks so much everyone! That makes the program look so much better. I think I may be starting to understand this a little better.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pyguy25 is offline Offline
10 posts
since Mar 2007
Feb 26th, 2010
0
Re: Need help with caesar cipher
I'm working on the same program, just modified slightly. Instead of printing the results, I'm writing them (and reading the input) from a .txt file. Unfortunatly, with a .txt file, it automatically adds a "\n" character at the end of each line. After running the program and opening the encoded message, there's an extra character at the end of the message. How can I pick that one character off?
Thanks so much!
Python Syntax (Toggle Plain Text)
  1. def main():
  2.  
  3. # gets user input to encode or decode
  4. decision = raw_input("Enter 1 to encode a message, and 2 to decode a message. ")
  5.  
  6. if int(decision) == 1: # starts encoding process if user enters 1
  7.  
  8. # creates files for reading and writing to
  9. infile = open("plaintext.txt", "r")
  10. outfile = open("ciphertext.txt", "w")
  11.  
  12. data = infile.read()
  13. key = int(data[0]) # "picks off" the shift key value from the text file and makes into an integer
  14.  
  15. message = data[1:] # slices off the shift key number to leave only the message to be encoded
  16. encrypted_message = ""
  17.  
  18. # creates a loop that goes through each character in the string
  19. # and adds the shift key value to that character's ascii value,
  20. # then turns each of those ascii values back into a character
  21. for i in message:
  22. letter_value = ord(i) + key
  23. if letter_value > ord("z"):
  24. letter_value -= ord("z") - ord("a") + 1
  25. encrypted_message = encrypted_message + chr(letter_value)
  26.  
  27. outfile.write(str(key)) # makes the key value a string and then writes to file
  28. outfile.write(encrypted_message) # writes encrypted message to file
  29.  
  30. infile.close() # closes infile
  31. outfile.close() # closes outfile
Reputation Points: 10
Solved Threads: 0
Newbie Poster
santasnotreal is offline Offline
1 posts
since Feb 2010
Feb 26th, 2010
0
Re: Need help with caesar cipher
Python Syntax (Toggle Plain Text)
  1. s = "abcdefg\n"
  2.  
  3. print s
  4. print '-'*10
  5.  
  6. # strip off trailing '\n' char
  7. s = s.rstrip('\n')
  8.  
  9. print s
  10. print '-'*10
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Feb 28th, 2010
0
Re: Need help with caesar cipher
Sounds interesting, so i put it all together:
Python Syntax (Toggle Plain Text)
  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:
Python Syntax (Toggle Plain Text)
  1. just a simple test!
  2. ugef l etxbwp fpef!
-- great, but how can I get the original message back?
Sorry im a n00b, would like to know that the for-loop does. Thanks
Reputation Points: 10
Solved Threads: 2
Light Poster
soUPERMan is offline Offline
36 posts
since Feb 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: An insertion sort for strings? how would I do this
Next Thread in Python Forum Timeline: Help on Program!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC