Mr. Jibz
Mr. Jibz

Reputation: 521

PYTHON basic ascii encryption function

When provided text as input string, and rule as input integer, challenge is to find ASCII value sum then convert back to string based on new numeric value.

My code appears on the right track, but given ascii_encrypt("a",1), for example, my current output is b'b' when it should be 'b'. I'm new to the encode function, which I'm guessing is tripping me up.

def ascii_encrypt(text, rule):
    text = sum([ord(c) for c in text])
    if not text:
        return ""
    else:
        encrypted_text = chr(text + rule)
        return encrypted_text.encode('utf-8')

Upvotes: 1

Views: 202

Answers (1)

Neil
Neil

Reputation: 14313

Simply remove, .encode('utf-8'). You don't need to encode it and it's causing your issue. You can't include this part and achieve desired functionality.

def ascii_encrypt(text, rule):
    text = sum([ord(c) for c in text])
    if not text:
        return ""
    else:
        encrypted_text = chr(text + rule)
        return encrypted_text

print(ascii_encrypt("a",1))

Upvotes: 2

Related Questions