1

I have this function that decodes a letter by a given key

def decode_char(n, key):
    adj = ord('a') if n.islower() else ord('A') 
    return chr(adj + (ord(n)-adj-int(key))%26)

I am trying to get this to work, so a word is decoded

def decode_block(word,key):
    letters = list(word)
    keys = list(key)
    decoded = []
    for letter, digit in zip(letters, keys):
        decoded.append(decode_char(letter, digit))
    return "".join(decoded)

when I input this

print(decode_char('bddffhhj', '12121212'))

I get this error message

TypeError: ord() expected a character, but string of length 8 found

I need to get

abcdefgh

I can't figure out why the ord(n) in decode_char isn't recieveing a character?

I have split the word into a list? and zipped these to the digits of the key?

Just a student so please don't go ham

Anyone?

  • What exactly does "it doesn't work" mean? The expected output and description of what the code does would help a lot here. EDIT: and avoid saying "it doesn't work" in future questions ;) – Gerry Hernandez Mar 12 '17 at 11:01
  • 1
    @GerryHernandez Edited above – Sam Kenneth Gordon Mar 12 '17 at 11:05
  • Incase you are looking for: [Caesar Cipher](http://stackoverflow.com/questions/8886947/caesar-cipher-function-in-python) – bhansa Mar 12 '17 at 11:10
  • @Bhansa This is for an assignment I have that everything has to be made from scratch, not allowed certain functions such as cipher etc. – Sam Kenneth Gordon Mar 12 '17 at 11:11
  • Sorry, but you need to understand how you are using your code_block and keys working together. Try different scenarios , work with examples. – bhansa Mar 12 '17 at 11:30
  • You've received two answers. Did any of those answers answer your question? If not, you can post your own answer. If they did, please [accept](https://meta.stackexchange.com/q/5234/266187) one of the answers. – Artjom B. Mar 12 '17 at 15:52

2 Answers2

0

The problem is with decode_char. It should be almost the same as code_char, except for subtracting the key integer instead of adding it. Here's a repaired version.

def decode_char(n, key):
    adj = ord('a') if n.islower() else ord('A') 
    return chr(adj + (ord(n)-adj-int(key))%26)

I don't understand why you have encrypt and code_block. You can get rid of encrypt and rename code_block to encrypt. The same goes for decrypt / decode_block.

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
0
def code_char(c, key):
    return chr(ord(c)+key%26)
code_char('a',3)

def decode_char(c,key):
    return chr(ord(c)-key%26)
decode_char('d',3)

Sample Example

txt = "This is a secret message!!"

#encoding
lista = []
for i in txt:
    lista.append(code_char(i,3))
print ''.join(lista)

encoded = ''.join(lista)

lista = []
#decoding
for i in encoded:
    lista.append(decode_char(i,3))
print ''.join(lista)

decoded = ''.join(lista)

Output:

Wklv#lv#d#vhfuhw#phvvdjh$$
This is a secret message!!
bhansa
  • 7,282
  • 3
  • 30
  • 55
  • While your code is simpler than the OP's your encoding / decoding process is rather different to what the OP is doing. Their algorithm only maps letter to letters of the same case and leaves other chars unchanged. – PM 2Ring Mar 12 '17 at 11:35
  • Hmm, I have doubts about his encoding algorithm. I'll update. – bhansa Mar 12 '17 at 11:45