That's a good start. I might do something like
...
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
jrcagle
Practically a Master Poster
608 posts since Jul 2006
Reputation Points: 92
Solved Threads: 156
Sounds interesting, so i put it all together:
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
-- and I get this:
just a simple test!
ugef l etxbwp fpef!
-- great, but how can I get the original message back?
Lardmeister
Posting Virtuoso
1,749 posts since Mar 2007
Reputation Points: 407
Solved Threads: 44
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
jrcagle
Practically a Master Poster
608 posts since Jul 2006
Reputation Points: 92
Solved Threads: 156
s = "abcdefg\n"
print s
print '-'*10
# strip off trailing '\n' char
s = s.rstrip('\n')
print s
print '-'*10
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
To:
soUPERMan
Solved threads are not a good media to ask questions. Many folks think the thread is solved and don't even look. So, if you have a question, start your own thread and title it properly.
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417