| | |
Need help with caesar cipher
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Mar 2007
Posts: 10
Reputation:
Solved Threads: 0
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:
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.
Python Syntax (Toggle Plain Text)
import string def main(): print "This program will encode your messages using a Caesar Cipher" key = input("Enter the key: ") message = raw_input("Enter the message: ") codedMessage = "" for ch in message: codedMessage = codedMessage + chr(ord(ch) + key) print "The coded message is:", codedMessage main()
Last edited by pyguy25; Mar 7th, 2007 at 7:24 pm.
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 150
That's a good start. I might do something like
Jeff
Python Syntax (Toggle Plain Text)
... for ch in message: code_val = ord(ch) + key if code_val > ord('z'): code_val -= 26 # better: ord('z') - ord('a'), just in case len(alphabet) != 26 codedMessage = codedMessage+chr(code_val) ...
Jeff
•
•
Join Date: Sep 2005
Posts: 133
Reputation:
Solved Threads: 58
Python Syntax (Toggle Plain Text)
... for char in message: # check if the character is alphabetic if char.isalpha(): ...
Regards, mawe
Sounds interesting, so i put it all together:
-- and I get this:
-- great, but how can I get the original message back?
Python Syntax (Toggle Plain Text)
message = "just a simple test!" key = 11 coded_message = "" for ch in message: code_val = ord(ch) + key if ch.isalpha(): if code_val > ord('z'): code_val -= ord('z') - ord('a') coded_message = coded_message + chr(code_val) else: coded_message = coded_message + ch print message print coded_message
Python Syntax (Toggle Plain Text)
just a simple test! ugef l etxbwp fpef!
![]() |
Similar Threads
- Word Association Game (Posting Games)
- Need help with Caesar Cipher program (Java)
- Help With Caesar Cipher/Shift (Pascal and Delphi)
- Help with replace method. (Java)
- Help Me Solve My Infinite Loop (C++)
- ROT-13 program (Python)
- Caesar cipher problem with CHR(32) (Visual Basic 4 / 5 / 6)
Other Threads in the Python Forum
- Previous Thread: Radiobutton text placement
- Next Thread: how to access dropdown list entries in python psp site
| Thread Tools | Search this Thread |
Tag cloud for Python
abrupt ansi anti approximation assignment avogadro backend basic beginner binary bluetooth calculator character code customdialog decimals dictionaries dictionary drive dynamic examples excel exe file float format ftp function gnu graphics gui heads homework http ideas import input java launcher leftmouse line linux list lists loop module mouse number numbers output parsing path pointer port prime program programming progressbar projects py2exe pygame pyqt python random recursion recursive refresh schedule scrolledtext sqlite ssh statistics stdout string strings sudokusolver sum table terminal text thread threading time tkinter tlapse tricks tuple tutorial twoup ubuntu unicode update urllib urllib2 variable wikipedia windows write wxpython xlib






