我試圖通過ascii索引來搜索caesar cipher程序,該範圍> = 32和< = 126。當我到達最後一個打印輸出時,我得到一些字符不在這個範圍內。我已經嘗試了循環和while循環,但不斷收到錯誤。使用索引範圍搜索ascii
我很抱歉,如果我沒有發佈這個正確。這是我的第一篇文章。
感謝您的任何幫助。
def cipher(phrase,shift):
x = list(str(phrase))
xx = ''.join(list(str(phrase)))
yy = []
print(x)
for c in xx:
yy.append(chr(ord(c)+shift))
return ''.join(yy)
print(cipher('ABCDE', 97 - 65))
print(cipher('abcde', 65 - 97))
print(cipher(' !\"#$', 95))
和我的輸出是:
['A', 'B', 'C', 'D', 'E']
abcde
['a', 'b', 'c', 'd', 'e']
ABCDE
[' ', '!', '"', '#', '$']
什麼是你指定'x'和'xx'行的目的是什麼?我不認爲你需要這些。你可以直接在'phrase'上迭代。 – 2017-10-21 22:01:17
爲什麼最後一次打印的轉換是95? – utengr
當你運行'print(cipher('!\「#$',95))'你會把所有的字符移到126以上。那麼你想要達到什麼效果? – gommb