Ask Your Question
0

pretty dumb question but I don't know

asked 2013-04-03 15:37:54 +0200

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

en_alphabet = "abcdefghijklmnopqrstuvwxyz"
def is_alphabetic_char(c):
    return (c.lower() in en_alphabet)
def char_to_num(c):
    return en_alphabet.index(c.lower())
def num_to_char(x):
    return en_alphabet[x % 26]

def CaesarEncrypt(k, plaintext):
    ciphertext=""
    for j in xrange(len(plaintext)):
        p= plaintext[j]
        if is_alphabetic_char(p):
            x= (k + char_to_num(p))%26
            c= num_to_char(x)
        else:
            c = p
            ciphertext += c
        return ciphertext

(but when I enter

k=16 ;plaintext = 'test.'
CaesarEncrypt(k,plaintext)

)

why does it reply me ' ' but ciphertext......

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2013-04-03 16:29:02 +0200

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

Your question and the quoted response from Sage you give aren't clear. But, I think you might have an indentation error in the last two lines of CaesarEncrypt. Make sure your code looks like:

def CaesarEncrypt(k, plaintext):
    ciphertext=""
    for j in xrange(len(plaintext)):
        p= plaintext[j]
        if is_alphabetic_char(p):
            x= (k + char_to_num(p))%26
            c= num_to_char(x)
        else:
            c = p
        ciphertext += c
    return ciphertext

This seems to be working okay. See this link to your code at the single cell server.

edit flag offensive delete link more

Comments

yeah u find the point!!

Aye gravatar imageAye ( 2013-04-03 16:48:04 +0200 )edit

yeah u find the point!! I'm really new to python and sage. Don't know that form bring much influence to the result, thank you very much

Aye gravatar imageAye ( 2013-04-03 16:48:04 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2013-04-03 15:37:54 +0200

Seen: 354 times

Last updated: Apr 03 '13