First time here? Check out the FAQ!

Ask Your Question
0

pretty dumb question but I don't know

asked 11 years ago

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......

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
2

answered 11 years ago

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.

Preview: (hide)
link

Comments

yeah u find the point!!

Aye gravatar imageAye ( 11 years ago )

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 ( 11 years ago )

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: 11 years ago

Seen: 431 times

Last updated: Apr 03 '13