當提供text
作爲輸入字符串,並且rule
作爲輸入整數時,挑戰是找到ASCII值sum,然後根據新的數值轉換回字符串。PYTHON basic ascii加密函數
我的代碼出現在正確的軌道上,但給出ascii_encrypt("a",1)
,例如,我的當前輸出是b'b'
,應該是'b'
。我是新來的編碼功能,我猜是讓我絆倒了。
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')
幫助想要!謝謝!
你爲什麼用UTF-8編碼結果? – user2357112
一個測試用例有混合字母,所以我認爲這將解釋爲 –
我不明白這行代碼的用法:'text = sum([ord(c)for c in text])'。你想用大於1個字符的字符串來使用這個函數嗎? –