3

I'm trying to have fun with one of my buddies, we are special agents but how can we be special agents without having top secret message code to communicate with each other?

# txt = the secret message to convert!
# n = number of times to jump from original position of letter in ASCII code

def spy_code(txt,n):
    result = ''
    for i in txt:
        c = ord(i)+n
        if ord(i) != c:
            b = chr(c)
            result += b
    print result

spy_code('abord mission, dont atk teacher!',5)

After converting message in secret message we are getting one line text...

fgtwi%rnxxnts1%itsy%fyp%yjfhmjw&

problem is, that we'd like to achieve such result:

fgtwi rnxxnts, itsy fyp yjfhmjw!

Considering only letters.

Only use spy code to convert letters and dont convert spaces or special symboles.

georg
  • 211,518
  • 52
  • 313
  • 390
user1768615
  • 281
  • 1
  • 5
  • 12
  • As a clue, it looks like you only want to convert if the character is a letter. A string of upper and lower case letters can be found in the `string` module. `import string; string.letters` – GP89 Nov 03 '12 at 16:03
  • 1
    I don't exactly get the neg votes :s. This is a correct question for me – jlengrand Nov 03 '12 at 16:07
  • @hayden, sorry, `a` is `c`, just corrected. Jlengrand They are like sheeps, lets join lets beeep, – user1768615 Nov 03 '12 at 16:12
  • 2
    Mocking them is not going to help you get upvotes :D – jlengrand Nov 03 '12 at 16:14
  • Yeah, have to say this is a pretty straightforward programming question (I voted to reopen), but jerky behavior is not going to inspire anyone to help you. – Larry Lustig Nov 03 '12 at 16:15
  • Hint: check `ord('A') <= ord(i) <= ord('z')` before converting. – Andy Hayden Nov 03 '12 at 16:17
  • I don't get why you don't want to convert special symbols. If you want a secret code, thats the best way to go, isn't it? – jlengrand Nov 03 '12 at 16:19
  • 1
    As a hint, your question would never have been closed with a title like "shift characters in string" or something else more general :) – jlengrand Nov 03 '12 at 16:24
  • sorry for "sheeps", if we convert full phrase then we wont be able to understand it in short time during class lessons =/ – user1768615 Nov 03 '12 at 16:27
  • @jlengrand probably not but I had very good day =P – user1768615 Nov 03 '12 at 16:29
  • 2
    hint: http://stackoverflow.com/search?q=caesar+%5Bpython%5D – georg Nov 03 '12 at 16:32
  • @thg435 thanks you now I know how it's called :) – user1768615 Nov 03 '12 at 16:38
  • 1
    If you're going to be special agents and you have a secure channel (the two of you actually meet each other physically), why don't you exchange your messages using [totally unbreakable encryption](http://en.wikipedia.org/wiki/One_time_pad) instead of [caesar cipher](http://en.wikipedia.org/wiki/Caesar_cipher) (which is extremely easy to break using frequency analysis). Example implementation of a one time pad (in numpy) is halfway down in [this answer](http://stackoverflow.com/a/9120498/566644) to a related question. :) – Lauritz V. Thaulow Nov 03 '12 at 17:22
  • Remember you need to "wrap" the lower letters around -- if you add one to "z" you don't get "a". – Katriel Nov 03 '12 at 17:22
  • 1
    for reference, [this code](http://stackoverflow.com/a/8895517/989121) seems the most straightforward way to implement the caeser cipher in python – georg Nov 03 '12 at 17:23
  • If you are doing this for fun - go ahead, but please-please don't use this for any production code (it's not secure by any stretch of imagination). – Victor Ronin Nov 05 '12 at 19:03
  • @VictorRonin I'm just having fun, im teenage and I didnt consider it lal :O – user1768615 Nov 14 '12 at 17:14

2 Answers2

2

A simple way to go, using the tip GP89 gave you.

Simply check if your current character is in the list of letter; otherwise just return it as it is

import string

vals = string.letters
def spy_code(txt,n):
    result = ''
    for i in txt:
        if i in vals:
            c = ord(i)+n
            if ord(i) != c:
                b = chr(c)
                result += b
        else:
            result += i
    print result

spy_code('abord mission, dont atk teacher!',5)

returns

fgtwi rnxxnts, itsy fyp yjfhmjw!
jlengrand
  • 12,152
  • 14
  • 57
  • 87
1

I know this is old, but you can use str.isalpha():

s = 'Impending doom approaches!'
n = 6
result = ''

for c in s:
    result += chr(ord(c) + n) if c.isalpha() else c

print(result)
>>> Osvktjotm juus gvvxuginky!
Necronomicron
  • 1,150
  • 4
  • 24
  • 47